0

I am new to struts2. I am using NetBeans8, Glassfish4 and MySql6.

I am making a login form. The problem I am having is is that I am not getting either an error or any output.

I can run my application and at my login page after clicking on submit nothing happens, but my url changes to http://localhost:8080/Pharma/loginAction.action

Please could somebody help, your suggestions would be highly appreciated

LoginAction.java

package package_login;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;


public class LoginAction extends ActionSupport implements ModelDriven<User>{
    User user=new User();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public User getModel() {   
       return user;
    }
     public void validate(){
     if(StringUtils.isEmpty(user.getEmail())){
         addFieldError("email","Please entered your email");
     }

      if(StringUtils.isEmpty(user.getPassword())){
         addFieldError("password","Please entered your password");
     }

     }


    public String execute() throws ClassNotFoundException, SQLException {
        LoginService loginService= new LoginService();
        if(loginService.verifyLogin(user)){
      return "logged";
}
return "login";
}
}

LoginService.java

package package_login;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


    class LoginService {
        public boolean verifyLogin(User user) throws ClassNotFoundException, SQLException{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/erp","root","ali$");
            Statement st= con.createStatement();
            ResultSet rs= st.executeQuery("select password,email from login where email='"+user.getEmail()+"'" );
            while(rs.next()){
           //   if(user.getEmail().equals(rs.getString("email"))&& user.getPassword().equals("password")){
            //    return true;
               }
           // }
            return false;
        }

    }

User.java

package package_login;


class User {
    private String email;
    private String password;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

login.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!-- Configuration for the default package. -->
    <package name="package_login" extends="struts-default">
        <action name="loginAction" class="package_login.LoginAction">
            <result name="logged">/welcome.jsp</result>
            <result name="login">/login.jsp</result>
            <result name="input">/login.jsp</result>
        </action>
    </package>
</struts>

login.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <s:form action="loginAction">
            <s:textfield key="email" label="Email"/>
            <s:password key="password" label="Password"/>
            <s:submit value="login"/>
        </s:form>
    </body>
</html>
Roman C
  • 49,761
  • 33
  • 66
  • 176
sufiyan ali
  • 71
  • 1
  • 1
  • 9
  • 1
    Check [this](http://stackoverflow.com/a/31490488/573032) answer how to handle parameters in sql/hql queries to avoid security vulnerabilities in the code. – Roman C Jul 29 '15 at 10:35

1 Answers1

1
ResultSet rs= st.executeQuery("select email from login where email='"+user.getEmail()+"' and password='"+user.getPassword()+"'" );
if(rs.next())
 {
      return true;//if valid user, return true
 }
 else 
 return false;

and your are using struts configuration file name login.xml.

The struts configuration file name instead of login.xml should use struts.xml

Instead of Statement, use PrepareStatement or callablestatement to avoid sql injection.

Link

Community
  • 1
  • 1
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31