Click here to Skip to main content
15,889,863 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

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

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
Location: http://www.example.org/
Content-Type: text/html
Content-Length: 174
 
<html>
<head>
<title>Moved</title>
</head>
<body>
<h1>Moved</h1>
<p>This page has moved to <a href="http://www.example.org/">http://www.example.org/</a>.</p>
</body>
</html>

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.
    HTML
    response.sendRedirect(request.getContextPath() + "/login2.jsp?loginMsg=" + URLEncoder.encode(message));

    Partial code for login2.jsp to display the message is shown below
    ASP.NET
    <%@ page info="Login" language="java" contentType="text/html;charset=utf-8" %<
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" 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.

Regards,
 
Share this answer
 
v2
Comments
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.

Regards,
mali_angel 9-Jul-13 3:06am    
login2.jsp

<%@page import="com.mobitel.bankdemo.web.UserControllerServlet"%>
<%@ 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>Insert title here</title>
</head>
<body>
<form name="input" action="login" method="post">

Login

<br>
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>
Register
</form>
</body>
</html>
Try EL(Expression Language) instead of jsp scriptlet. Simply put
HTML
${loginMsg}
in your logout page. To learn more ...
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html[^]
 
Share this answer
 
v2
Comments
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
 
Comments
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");
dispatcher.forward(request,response);
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 ...:)
login.jsp
HTML
<![CDATA[<%@page import="com.mobitel.bankdemo.web.UserControllerServlet"%>
<![CDATA[<%@ 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>Insert title here</title>
</meta></head>
<body>
	<form name="input" action="login.do" method="post">

Create the servlet with url like login.do
Java
public class Login extends HttpServlet {

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

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

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