Thursday, 20 October 2011

Struts Login Application with JDBC connection


********Just create as Dynamic web project in eclipse and run using tomcat server*********

1. add lib, tld  folder and web.xml, struts-config.xml inside WEB-INF
2.index.jsp , homepage.jsp, userUnlog.jsp
3. <%@taglib uri="/WEB-INF/tld/struts/struts-html.tld" prefix="html" %>  in welcome.jsp
4. LoginForm, LoginAction, Loginservice, LoginServiceImpl



Folder Structure of StrutsApplication
LoginApplication
*src
----com.action -> LoginAction.java
----com.form   -> LoginForm.java
----com.service -> LoginService.java
                 -> LoginServiceImpl.java
*webcontent
----WEB-INF
-> lib
          -->> antlr.jar, commons-beanutils, coms-digester,
           coms-fileupload, coms-logging, coms-validator,
                   jakarta-oro, jstl, ojdbc14, standard, struts.jar, struts-el.jar
-> tld --->> struts
             ---->>> struts-html.tld
             ---->>> struts-logic.tld
-> struts-config.xml
-> web.xml
----homepage.jsp
----index.jsp
----userUnlog.jsp


**********  web.xml **************

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>StrutsApplication1</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>120</session-timeout>
</session-config>

<jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>http://jakarta.apache.org/struts/tags-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-logic.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>

**********  struts-config.xml **************

<?xml version="1.0" encoding="UTF-8"?>
<!-- ?xml version="1.0" encoding="ISO-8859-1" ?-->

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
<form-beans>
<form-bean name="LoginForm" type="com.form.LoginForm" />
</form-beans>

<action-mappings>
<action input="/index.jsp" name="LoginForm" path="/Login"
scope="session" type="com.action.LoginAction">
<forward name="SUCCESS" path="/homepage.jsp" />
<forward name="FAILURE" path="/userUnlog.jsp" />
</action>
</action-mappings>
</struts-config>

**********  LoginForm.java**************

package com.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm{
private static final long serialVersionUID = 1L;
String userName;
String password;

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

**********  LoginService.java**************

package com.service;
import java.util.List;
public interface LoginService {
public boolean userValidation(String username, String userpass);
}


**********  LoginServiceImpl.java**************

package com.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.form.LoginForm;
public class LoginServiceImpl implements LoginService {
public boolean userValidation(String username, String userpass){

System.out.println("Username & password : " + username + " " + userpass);
String strQuery= "";
        boolean validUser = false;
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "db_username", "db_password");
Statement statement = connection.createStatement();
strQuery="select * from user_table where username='"+username+"' and  password='"+userpass+"'";
System.out.println(strQuery);  

ResultSet rs = statement.executeQuery(strQuery);
rs = statement.executeQuery(strQuery);
System.out.println("Result : " + rs);
            boolean more = rs.next();

            if (!more) {
            validUser = false;
      }else if (more) {
            validUser = true;
            }
}catch (Exception ex) {
            System.out.println("Log In failed: An Exception has occurred! " + ex);
}
return validUser;
}
}

**********  LoginAction.java**************

 package com.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.form.LoginForm;
import com.service.LoginService;
import com.service.LoginServiceImpl;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

LoginForm loginForm = (LoginForm) form;
String username = loginForm.getUserName();
String password = loginForm.getPassword();

HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser", username);

LoginService loginService = new LoginServiceImpl();
boolean isValidUser = loginService.userValidation(username, password);

if (isValidUser) {
return mapping.findForward("SUCCESS");
} else {
return mapping.findForward("FAILURE");
}
}
}


**********  index.jsp **************

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/tld/struts/struts-html.tld" prefix="html" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts Example</title>
</head>
<body>
<center>
<html:form action="/Login" method="post">
        User Name :<html:text property="userName" /><br>
        Password  :<html:password property="password" /><br>
        <html:submit value="Login" />
    </html:form>
    </center>
</body>
</html>


**********  homepage.jsp **************

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title> User Logged Successfully </title>
</head>
<body>
<center>
<% String user = (String)session.getAttribute("currentSessionUser"); %>
Welcome <%= user %> !!
</center>
</body>
</html>


**********  userUnlog.jsp **************

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Invalid user</title>
</head>
<body>
<center>
Sorry, you are not a registered user! Please sign up first..
</center>
</body>
</html>

************************************************************************

No comments:

Post a Comment