get value for your money - 77612

Solution Detail
Price: $50.00
Request Description
0
Solution Description

login.html is the entry point of our application where user will provide login id and password for authentication.

login.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!DOCTYPE html>

<html>

<head>

<meta charset="US-ASCII">

<title>Login Page</title>

</head>

<body>

 

<form action="LoginServlet" method="post">

 

Username: <input type="text" name="user">

<br>

Password: <input type="password" name="pwd">

<br>

<input type="submit" value="Login">

</form>

</body>

</html>

LoginServlet is used to authenticate the request from client for login.

LoginServlet.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

package com.journaldev.servlet.session;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

/**

 * Servlet implementation class LoginServlet

 */

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private final String userID = "admin";

    private final String password = "password";

 

    protected void doPost(HttpServletRequest request,

            HttpServletResponse response) throws ServletException, IOException {

 

        // get request parameters for userID and password

        String user = request.getParameter("user");

        String pwd = request.getParameter("pwd");

         

        if(userID.equals(user) && password.equals(pwd)){

            HttpSession session = request.getSession();

            session.setAttribute("user", "Pankaj");

            //setting session to expiry in 30 mins

            session.setMaxInactiveInterval(30*60);

            Cookie userName = new Cookie("user", user);

            userName.setMaxAge(30*60);

            response.addCookie(userName);

            response.sendRedirect("LoginSuccess.jsp");

        }else{

            RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");

            PrintWriter out= response.getWriter();

            out.println("<font color=red>Either user name or password is wrong.</font>");

            rd.include(request, response);

        }

 

    }

 

}

When client is authenticated, it’s forwarded to LoginSuccess.jsp

LoginSuccess.jsp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<%@ page language="java" contentType="text/html; charset=US-ASCII"

    pageEncoding="US-ASCII"%>

<!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=US-ASCII">

<title>Login Success Page</title>

</head>

<body>

<%

//allow access only if session exists

String user = (String) session.getAttribute("user");

String userName = null;

String sessionID = null;

Cookie[] cookies = request.getCookies();

if(cookies !=null){

for(Cookie cookie : cookies){

    if(cookie.getName().equals("user")) userName = cookie.getValue();

    if(cookie.getName().equals("JSESSIONID")) sessionID = cookie.getValue();

}

}

%>

<h3>Hi <%=userName %>, Login successful. Your Session ID=<%=sessionID %></h3>

<br>

User=<%=user %>

<br>

<a href="CheckoutPage.jsp">Checkout Page</a>

<form action="LogoutServlet" method="post">

<input type="submit" value="Logout" >

</form>

</body>

</html>

Notice that there is no session validation logic in the above JSP. It contains link to another JSP page, CheckoutPage.jsp.

CheckoutPage.jsp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<%@ page language="java" contentType="text/html; charset=US-ASCII"

    pageEncoding="US-ASCII"%>

<!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=US-ASCII">

<title>Login Success Page</title>

</h