1

I am creating a struts2 application. I want users to login to their account. If the record is found, it should go to the success.jsp. If the account is not found, it should default to to error.jsp. They can then register. The problem I am having is that whether the records are in the database or not, I get redirected to my error.jsp. I would appreciate some help here with some like explanation to go with it

Here are my files:

StudentInfo

package org.comp.dto;

//import

    public class StudentInfo implements Serializable{
        private int studentId;
        private String userName;
        private String password;
        private String password1;
        private String firstName;
        private String lastName;
        private int age;
        private String dateofBirth;
        private Calendar dateCreated;
        private GregorianCalendar lastLogin;


        public int getStudentId() {
            return studentId;
        }
        public void setStudentId(int studentId) {
            this.studentId = studentId;
        }
        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;
        }
        public String getPassword1() {
            return password1;
        }
        public void setPassword1(String password1) {
            this.password1 = password1;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getDateofBirth() {
            return dateofBirth;
        }
        public void setDateofBirth(String dateofBirth) {
            this.dateofBirth = dateofBirth;
        }

        public GregorianCalendar getLastLogin() {
            return lastLogin;
        }
        public void setLastLogin(GregorianCalendar lastLogin) {
            this.lastLogin = lastLogin;
        }
        public Calendar getDateCreated() {
            return dateCreated;
        }
        public void setDateCreated(Calendar dateCreated) {
            this.dateCreated = dateCreated;
        }

        }

StudentInfo.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
 <class mutable="true" name="org.comp.dto.StudentInfo" table="studentinfo">

        <id name="studentId" type="int">
            <column name="studentId"/>
               <generator class="native"/>
        </id>

        <property column="userName" name="userName" type="string" not-null="true"/> 
        <property column="password" name="password" type="string" not-null="true"/>
        <property column="firstName" name="firstName" type="string" not-null="true"/>
        <property column="lastName" name="lastName" type="string" not-null="true"/>
        <property column="age" name="age" type="int" not-null="true"/>
        <property column="dateofBirth" name="dateofBirth" type="string" not-null="true"/>
        <property column="dateCreated" name="dateCreated" type="calendar" not-null="true"/>
        <property column="lastLogin" name="lastLogin" type="timestamp" not-null="true"/>
    </class>
</hibernate-mapping>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts>
    <package name="loginpackage" namespace="/" extends="struts-default">
         <action name="login" class="org.action.LoginAction" method="login">
            <result name="success">success.jsp</result>
            <result name="input">login.jsp</result>
            <result name="error">error.jsp</result>

         </action>  

         <action name="accountSetUpAction" class="org.action.LoginAction" method="accountSetUp">
            <result name="success">success.jsp</result>
         </action>
    </package> 
</struts>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!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>Success page</title>
</head>
<body>
Login successfully! <s:property value="firstName"/>
</body>
</html>

LoginAction class

package org.action;

//import  

public class LoginAction extends ActionSupport implements ModelDriven{
    private int studentId;
    private String userName;
    private String password;
    private String firstName;
    private StudentInfo studentUser = new StudentInfo();
    private Session session;
    private String message;
    PreparedStatement sQry;
    ResultSet rs;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    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;
    }

    public StudentInfo getStudentUser() {
        return studentUser;
    }

    public void setStudentUser(StudentInfo studentUser) {
        this.studentUser = studentUser;
    }

    public LoginAction() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public void validate() {
        if (StringUtils.isEmpty(studentUser.getUserName())){
             addFieldError("userName", "Username cannot be blank");
         }
        if (StringUtils.isEmpty(studentUser.getPassword())){
             addFieldError("password", "Password cannot be blank");

        }
     }

    @Override
    public String execute() throws Exception {
            return SUCCESS; 
        }

    public String login(){
        String ret = ERROR;
        Connection conn = null;
        try {

            SessionImplementor impl = (SessionImplementor)session;
            conn = impl.getJdbcConnectionAccess().obtainConnection();
            org.hibernate.Transaction tx = session.beginTransaction();

            String sQry = "SELECT firstName FROM StudentInfo WHERE studentinfo.studentId=? AND studentinfo.userName=? AND studentinfo.password=?";
            PreparedStatement q = conn.prepareStatement(sQry);


            q.setInt(1, studentId);
            q.setString(2, userName);
            q.setString(3, password);


            rs = q.executeQuery();

            while (rs.next()){
                rs.getString(2);

                ret = SUCCESS;
            }


        }
                catch(Exception ex){
            ret = ERROR;
        }

        finally
        {
            if (conn !=null){
                try {
                    session.getTransaction().rollback();
                    conn.close();
                }
                catch (Exception ex){

                }
            }

        }
        return ret;

        }


    public String accountSetUp() throws Exception{
        Transaction tx = null;
        try {
        tx = session.beginTransaction();

        Calendar dateCreated = new GregorianCalendar();
        studentUser.setDateCreated(dateCreated);
        session.save(studentUser);
        session.getTransaction().commit();      

        return SUCCESS;
    }
        catch (HibernateException ex){
         if(tx != null) tx.rollback();
         ex.printStackTrace();
         return null; 

        }

        finally
        {
            session.close();

        }
        }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Override
    public Object getModel() {
        return studentUser;
    }

    }

The login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
   <%@ taglib uri="/struts-tags" prefix="s"%> 
<!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>Login Page</title>
</head>
<body>
   <s:form action="login" method="get">
       <s:textfield label="Please StudentID:" key="studentId"/>
       <s:textfield label="Please Enter User Name:" key="userName"/>
       <s:password label="Please Enter Password:" key="password"/>
       <s:submit/>
   </s:form>
</body>
</html>
CODI
  • 147
  • 4
  • 17

1 Answers1

1

In your LoginAction class:

String sQry = "SELECT firstName FROM StudentInfo WHERE studentinfo.userName=? AND studentinfo.password=?";
        PreparedStatement q = conn.prepareStatement(sQry);


        q.setInt(1, studentId);
        q.setString(2, userName);
        q.setString(3, password);

In the query you have two parameters (userName, password). But you are trying to set three after (studentId, userName, password).

To see the error in console, write ex.printStackTrace(); in your catch blocks (ex is your Exception name).

The Guest
  • 698
  • 11
  • 27
  • The Guest, I apologize about that query and I have edited it. The problem still remains though. – CODI May 08 '15 at 14:09
  • @CODI 1) Try to debug the application. 2) Try writing sysout statements before the which should possibly throw error 3) I prefer - if you can printStackTrace() the exception and show us the error message then it would be helpful – The Guest May 08 '15 at 14:12
  • I couldn't paste all the stace but here's the pertinent one: org.hibernate.HibernateException: getJdbcConnectionAccess is not valid without active transaction – CODI May 08 '15 at 14:26
  • org.hibernate.HibernateException: getJdbcConnectionAccess is not valid without active transaction at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) at com.sun.proxy.$Proxy6.getJdbcConnectionAccess(Unknown Source) at org.action.LoginAction.login(LoginAction.java:103) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) – CODI May 08 '15 at 14:38
  • See this post : http://stackoverflow.com/questions/9482731/how-to-get-jdbc-connection-from-hibernate-session AND http://stackoverflow.com/questions/3526556/session-connection-deprecated-on-hibernate – The Guest May 08 '15 at 14:42
  • And please check the way you are establishing the database connection properly – The Guest May 08 '15 at 14:44
  • The Guest, thanks for taking me though the steps. Jani thanks also – CODI May 08 '15 at 14:46