Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java JSP Servlet
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();
        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 8-Jul-13 18:58pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
    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="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,
  Permalink  
v2
Comments
mali_angel at 9-Jul-13 2:00am
   
why URLEncoder.encode(message)); line's encode word cut by a line?
Prasad Khandekar at 9-Jul-13 2:24am
   
Didn't get you?
mali_angel at 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 at 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 at 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>
Prasad Khandekar at 9-Jul-13 3:18am
   
Hello,
 
In your page you are using request.getAttribute("loginMsg");. As I explained earlier in your scenario it's not going to work, after redirect there won't be any attribute named loginMsg in request object. Please see the updated sample or you can change your line to
 
<p><%=request.getParameter("loginMsg")%></p>
 
Regards,
mali_angel at 9-Jul-13 10:44am
   
thank u :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try EL(Expression Language) instead of jsp scriptlet. Simply put
${loginMsg}
in your logout page. To learn more ...
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html[^]
  Permalink  
v2
Comments
mali_angel at 9-Jul-13 2:35am
   
this is too complex for a beginner like me :(
Shubhashish_Mandal at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
<![CDATA[<% String msgFormServlet = (String) request.getAttribute("SessionAttributes");%>]]>
<![CDATA[<% if (msgfomrServlet != null){
out.println("Logged");
else{
out.println("Session has been expired");
%>]]>
  Permalink  
Comments
mali_angel at 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 at 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 at 9-Jul-13 10:43am
   
thank u all ...:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

login.jsp
<![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
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
<![CDATA[<% String msgFormServlet = (String) session.getAttribute("loginMsg");%>
  <% if (msgfomrServlet != null){
out.println("Logged");
else{
out.println("Session has been expired");
%></form></body></html>
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 10 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100