Not going on to success page. Please tell me where I'm going wrong. 1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<welcome-file-list>
<welcome-file>/Login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
2.Login.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<link href="css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
<html:form action="loginaction" method="post">
<html:textfield name="emp_id" label="Employee ID"/>
<html:password name="Pwd" label="Password"/>
<html:checkbox label="Remember" name="checkboxField1" value="aBoolean" fieldValue="true"/>
<html:submit value="login"></html:submit>
</html:form>
</body>
</html>
3.LoginAction: I think the problem is here. pelase see in details and let me know
package controller;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class LoginAction extends ActionSupport {
String Emp_id;
String Pwd;
public String getEmp_id() {
return Emp_id;
}
public void setEmp_id(String Emp_id) {
this.Emp_id = Emp_id;
}
public String getPwd() {
return Pwd;
}
public void setPwd(String Pwd) {
this.Pwd = Pwd;
}
@Override
public String execute() throws ClassNotFoundException{
HttpServletRequest req = ServletActionContext.getRequest();
setEmp_id(req.getParameter("Emp_id"));
setPwd(req.getParameter("Pwd"));
LoginDao ld = new LoginDao();
if (ld.checkLogin(getEmp_id(), getPwd())) {
return SUCCESS;
} else
return ERROR;
}
@Override
public void validate() {
if("".equals(getEmp_id())){
addFieldError("Emp_id", "ID must be filled !");
}
if("".equals(getPwd())) {
addFieldError("Pwd", "Password must be filled !");
}
}
}
4.LoginDao:
package controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginDao {
public boolean checkLogin(String Emp_id, String Pwd) {
boolean status = false;
Connection conn = null;
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/timesheetdb", "root", "lion");
String query = "select Emp_id,Pwd from employee where Emp_id=?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, Emp_id);
ps.setString(2, Pwd);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
if (rs.getString("Emp_id").equals(Emp_id)&& (rs.getString("Pwd").equals(Pwd))) {
status = true;
} else {
status = false;
}
}
} catch (SQLException e) {
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return status;
}
}
5.Struts.xml:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="controller" extends="struts-default">
<action name="loginaction" class="controller.LoginAction">
<result name="input">/Login.jsp</result>
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
your variable name should match with the tags name attribute. replace your current variable.
private String emp_id;
and generate new getters and setters for this variable. you don't need request.getParameter() here, struts2 will do it internally.
@Override
public String execute() throws ClassNotFoundException
{
LoginDao ld = new LoginDao();
if (ld.checkLogin(getEmp_id(), getPwd())) // replace with new getters() of emp_id and Pwd
{
return SUCCESS;
}
else
return ERROR;
}
LoginDao: In your query you forget to pwd field compare where pwd = ? and you don't need equals check if result set is not empty return true. check posted code
String query = "select Emp_id,Pwd from employee where Emp_id=? and Pwd=?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, Emp_id);
ps.setString(2, Pwd);
ResultSet rs = ps.executeQuery();
if(rs.next())
return true;
else
return false;