Search code examples
javastruts

Struts application giving blank page


I'm trying to get user's email and password , upon form submission it checks up with the database if the email and password exists , if it exists it get's forwarded to success.jsp else failure.jsp But, when I submit the form it's giving me a blank page please help me out

login.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html:html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Login page</title>        
 </head>
 <body style="background-color: white">

 <html:form action="/login">
    <html:errors property="wrongcred" />
    
                 Email:
                 <html:text property="email" /><br><br>
                 Password:
                 <html:password property="pass" /><br><br>

     <html:submit value="Login" />
    </html:form>

    </body>
   </html:html> 

LoginAction.java

package com.myapp.struts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends org.apache.struts.action.Action {


private static final String SUCCESS = "success";
private static final String FAILURE = "failure";

private String email,pass,name;
 
   public String execute() {
  String ret = FAILURE;
  Connection conn = null;

  try {
     String URL = "jdbc:mysql://localhost:3306/studentdb";
     Class.forName("com.mysql.jdbc.Driver");
     conn = DriverManager.getConnection(URL, "root", "root");
     String sql = "select name from logincred where";
     sql+=" email = ? and pass = ?";
     PreparedStatement ps = conn.prepareStatement(sql);
     ps.setString(1, email);
     ps.setString(2, pass);
     ResultSet rs = ps.executeQuery();

     while (rs.next()) {
        name = rs.getString(1);
        ret = SUCCESS;
     }
  } catch (Exception e) {
     ret = FAILURE;
  } finally {
     if (conn != null) {
        try {
           conn.close();
        } catch (Exception e) {
        }
     }
  }
  return ret;
  }

   public String getEmail() {
    return email;
   }

   public String getName() {
    return name;
   }


    public void setEmail(String login) {
    login = email;
    }
    public void setName(String user){
    user = name;
    }
    public String getPass() {
    return pass;
    }


    public void setPass(String password) {
    password = pass;
    }


    }

struts.config.xml

<?xml version="1.0" encoding="UTF-8" ?>

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


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

 </form-beans>

 <global-exceptions>

 </global-exceptions>

 <global-forwards>
 <forward name="login" path="/Login.do"/>
 </global-forwards>

   <action-mappings>
   <action input="/login.jsp" name="LoginActionForm" path="/login" scope="request" 
   type="com.myapp.struts.LoginAction">
       <forward name="success" path="/success.jsp"/>
       <forward name="failure" path="/failure.jsp"/>
    </action>
   <action path="/Login" forward="/login.jsp"/>
   </action-mappings>
   <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

   <message-resources parameter="com/myapp/struts/ApplicationResource"/>    

   <plug-in className="org.apache.struts.tiles.TilesPlugin" >
    <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
    <set-property property="moduleAware" value="true" />
    </plug-in>

    <!-- ========================= Validator plugin ================================= -->
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>

    </struts-config>

Solution

  • update the execute method signature as below,

        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String ret = FAILURE;
        Connection conn = null;
    
        try {
            String URL = "jdbc:mysql://localhost:3306/studentdb";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, "root", "root");
            String sql = "select name from logincred where";
            sql += " email = ? and pass = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, email);
            ps.setString(2, pass);
            ResultSet rs = ps.executeQuery();
    
            while (rs.next()) {
                name = rs.getString(1);
                ret = SUCCESS;
            }
        } catch (Exception e) {
            ret = FAILURE;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
        return mapping.findForward(ret);
    }