0

On index page, jsp is not checking whether session is exists or not and continued to next page and after refreshing it shows same page with session attribute.

Actually in mycode if loop is not working what is problem

Please help out.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>login Page</title>
    </head>
    <body>
        <h1>index page</h1>
        <jsp:include page="/check" />       
    </body>
</html>

Servlet code:

package reg;

import java.io.IOException;
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;

public class check extends HttpServlet {  

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    response.setContentType("text/html");  
    PrintWriter out=response.getWriter();  
    //request.getRequestDispatcher("link.html").forward(request, response);  
    out.print("hello check servlet<br>");
    HttpSession nsession=request.getSession(false);

    if(nsession == null) {  
        String name=(String)nsession.getAttribute("name");
        out.print("Hello, "+name+" Welcome to Profile");
        request.getRequestDispatcher("/adminhome.jsp").include(request, response);
    } else {    
        out.print("Please login first");
        request.getRequestDispatcher("/login.jsp").include(request, response);
    }
    out.close();  
    }  
} 
Naman
  • 2,205
  • 2
  • 19
  • 32
Sagar y
  • 1
  • 1

1 Answers1

0

I also dont know what the problem is. It should be returning null, if you havent created a session elsewhere.

But try:

  • Unchecking Preserve sessions across redeployment, and see if that helps. (didnt help with me)
  • Or just code it differently like so:
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();
    out.print("<h2>hello check servlet</h2><hr>");
    HttpSession nsession = request.getSession();
    String name = (String) nsession.getAttribute("name");

    if(name != null) { 
        out.print("Hello, " + name + " Welcome to Profile");
        request.getRequestDispatcher("/adminhome.jsp").include(request, response);
    } else {
        out.print("Please login first!");
        request.getRequestDispatcher("/login.jsp").include(request, response);
    }
    out.close();  
}

Besides that, you do have another servlet which handles the login and if successfull set the session right? Something like this:

private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String email = request.getParameter("email");
    String password = request.getParameter("password");

    try {
        // Do database related stuff here... (to check if user exists)
        if(validUser())
            request.getSession().setAttribute("username", name); // Set the name of the user to the session
    }

You should also check these for more info on sessions, java session management http://www.tutorialspoint.com/servlets/servlets-session-tracking.htm.

Community
  • 1
  • 1
Yoshua Nahar
  • 1,304
  • 11
  • 28