Click here to Skip to main content
15,746,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this code 'loginMsg' displays 'null'. can anyone know why?
Expected outcome is when user logout it should invalidate session and go to login page with "Logout!" message.How to do that? I'm Using Java

private void logoutSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        HttpSession  session = request.getSession();
            String message = "Logout!";
            request.setAttribute("loginMsg", message);
            response.sendRedirect(request.getContextPath() + "/login2.jsp");
        }catch(Exception e){

Hello Mali,

Since you are doing a sendRedirect, the browser is typically going to receives 301 response as shown below. This response instructs the browser that the requested resource has been moved to a different location and the browser should make a new request to the URL specified in the location attribute of the response. Thus the browse generates a new request and sends it to the server, that's the precise reason why the value of loginMsg attribute is null in the new request.
Raw Redirect Ressponse
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Content-Length: 174
<p>This page has moved to <a href=""></a>.</p>

To be able to display the loginMsg after redirect you can do it in two ways.

  1. Put the loginMsg as part of the URL as shown below. Then in your login2.jsp retrieve this message via queystring (HttpServletRequest.getparameter(String)) and display it.
    response.sendRedirect(request.getContextPath() + "/login2.jsp?loginMsg=" + URLEncoder.encode(message));

    Partial code for login2.jsp to display the message is shown below
    <%@ page info="Login" language="java" contentType="text/html;charset=utf-8" %<
    <%@ taglib uri="" prefix="c"%>
    <c:out value="${param.loginMsg}"/>
    // Your remaining HTML Code
  2. Add the login message in HttpSession (just before the redirect, after invalidating the current session) and retrieve this value in login2.jsp to display the message.
    Note: Please don't forget to call session.invalidate() in your login servlet before you actually login.

Share this answer
mali_angel 9-Jul-13 2:00am    
why URLEncoder.encode(message)); line's encode word cut by a line?
Prasad Khandekar 9-Jul-13 2:24am    
Didn't get you?
mali_angel 9-Jul-13 2:34am    
Your both answers did not work for me. both displays 'null' before login. once user logged it displays correct message. i want to stop displaying 'null' word before display correct message.
Prasad Khandekar 9-Jul-13 2:43am    
Can you share the code for login2.jsp? I have updated the solution to show how it can be done in for the first scenario.

mali_angel 9-Jul-13 3:06am    

<%@page import="com.mobitel.bankdemo.web.UserControllerServlet"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form name="input" action="login" method="post">


UserName: <input type="text" name="txtUname"><br>
Password: <input type="password" name="txtPwrd"><br>

<input type="submit" value="Login" önclick= >
<input type="hidden" name="operation" value="login">

<p><%=request.getAttribute("loginMsg") %></p>
Try EL(Expression Language) instead of jsp scriptlet. Simply put
in your logout page. To learn more ...[^]
Share this answer
mali_angel 9-Jul-13 2:35am    
this is too complex for a beginner like me :(
Shubhashish_Mandal 9-Jul-13 2:42am    
This is not so tough. But to follow mvc you have to familiar with this. In a real world people enforce you to do that instead of jsp scriptlet. And as a beginner you have to start with this.
In the loginpage get the username and password like request.getParameter("fieldname");
and set the values in session parameters like session.setAttribute("fieldname");
Get the values in logout servlet and remove by session.invalidate method.

Use Sriptlets like this in login2.jsp
Share this answer
mali_angel 9-Jul-13 2:37am    
this is correct. but problem is there are different messages in different servlets which should display in the login.jsp page. for this scenario how to do that?
shanmuga1509 9-Jul-13 3:40am    
Then in servlets which you want to send message to jsp use this code
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/YourFile.jsp");
Then simply put request.getAttribute in yourfile.jsp
You will get all the assigned values in different servlets
mali_angel 9-Jul-13 10:43am    
thank u all ...:)
<![CDATA[<%@page import="com.mobitel.bankdemo.web.UserControllerServlet"%>
<![CDATA[<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
	<form name="input" action="" method="post">

Create the servlet with url like
public class Login extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException{
   String inputMsg = request.getParameter("input");  
   HttpSession session = request.getSession(true);
   RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login2.jsp");

In the login2.jsp type this
<![CDATA[<% String msgFormServlet = (String) session.getAttribute("loginMsg");%>
  <% if (msgfomrServlet != null){
out.println("Session has been expired");
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900