-2

I have two pages jsp pages First one is AppLogin.jsp and other one is Login.jsp

I want that when i click on Submit button of AppLogin.jsp page, i want redirect to Login.jsp page with password value and check this value on page load of Login.jsp page.

For this i did following code but the below code did not work.

I don't want to use Session and append value on url.

AppLogin.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>App Login</title>
</head>
<body>
    <form action="loginServlet" method="post">
        <table>
            <tr>
                <td>Application Name</td>
                <td><input type="text" value="" name="txtApplication" /></td>
            </tr>
            <tr>
                <td>Username</td>
                <td><input type="text" value="" name="txtUsername" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="text" value="" name="txtPassword" /></td>
            </tr>
            <tr align="center">
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, ClientProtocolException {
        try {
            String password = req.getParameter("txtPassword");
            req.setAttribute("crendential",password);
            RequestDispatcher rd = req.getRequestDispatcher("Login.jsp");
            rd.forward(req,res);
        }

        catch (Exception e) {
            System.out.println("LoginServletException>>>>>>>>>>" + e);
        }
    }
}

Login.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>Login</title>
</head>
<body>
    <form action="saveServlet" method="post">
        <table>
            <tr>
                <td>Application :</td>
                <td><input type="text" value="" name="txtApplication" /></td>
            </tr>
            <tr>
                <td>Username :</td>
                <td><input type="text" value="" name="txtUsername" /></td>
            </tr>
            <tr>
                <td>Password :</td>
                <td><input type="text" value="" name="txtPassword" /></td>
            </tr>
            <tr align="center">
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

public class SaveServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException {
        try {
            String crendential = (String) req.getAttribute("crendential");
            System.out.println("crendential>>>>>>>>>>" + crendential);
        }

        catch (Exception e) {
            System.out.println("LoginServletException>>>>>>>>>>" + e);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String crendential = (String) req.getAttribute("crendential");
        System.out.println("crendential>>>>>>>>>>" + crendential);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>xyz</display-name>
    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>xyz.io.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/loginServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>save</servlet-name>
        <servlet-class>xyz.io.SaveServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>save</servlet-name>
        <url-pattern>/Login.jsp</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>AppLogin.jsp</welcome-file>
    </welcome-file-list>
</web-app>

I am using Java with Eclipse Mars.

user3441151
  • 1,880
  • 6
  • 35
  • 79

1 Answers1

1

Login.jsp has a request attribute "credentials." To pass this value to the saveServlet there would be two possibilities:

  • Store the credentials instead as session attribute
  • Pass the credentials on in Login.jsp, say as hidden form input field.

    <input type="hidden" name="credentials" value="${credentials}">
    

Correction:

I did miss the somewhat hard to digest web.xml with its servlet mappings.

In general how it often is done

  • A servlet mapping:

    xyz.io.DisplayLoginServlet /login

    xyz.io.ProcessLoginServlet /authorize

  • DisplayLoginServlet

    doGet prepares the form data (request attributes) forwards to some displayLogin.jsp

    doPost validates the form, possibly redisplays the form with error messages (doGet) otherwise when successfully done, set session attributes and redirect to the home page.

  • displayLogin.jsp

    (posts to the same servlet)

And so on.

The mapped servlet URLs do not relate to JSPs. The servlet is the entry point, the controller. It prepares the data model, puts them in the request attributes, and forwards to some JSP, the view. The servlet can on successfull login use an other JSP for instance.

Here we redirect after a successfull post. That is the brower is told to get the redirected page as answer. The effect is that the user cannot reload the page and effectively re-post the HTML form.

I think making a clean distinction will already help.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138