javahtmljspjspinclude

jsp include does not work


login.html
this file contains code for 2 buttons.The sign in and sign up button which are 2 forms..
connected.jsp
this file contains code for 1 button the one which the user can click and log out.
Controller.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="something.*" %>
<%@ page errorPage="Error.jsp" %>

<% 

request.setCharacterEncoding("ISO-8859-7");
String errorMessages = "";
String username = request.getParameter("username");
String password = request.getParameter("password");
if ((password.length() > 0) && !(username.length() > 0)) {
    errorMessages = ("message1");
    throw new Exception(errorMessages);
}
if ((username.length() > 0) && !(password.length() > 0)) {
    errorMessages = ("message2"); 
    throw new Exception(errorMessages);
}
if (!(password.length() > 0) && !(username.length() > 0)) {
    errorMessages = ("message3");
    throw new Exception(errorMessages);
}
DB_something db = new DB_something ();
db.open();
if (request.getParameter("FistName") == null) {
    db.authenticateUser(username, password);
    session.setAttribute("login_status", "connected");

} else {
    String fName = request.getParameter("FistName");
    String lName = request.getParameter("LastName");
    String email = request.getParameter("email");
    String gender = request.getParameter("sex");
    String month = request.getParameter("month");
    String day = request.getParameter("day");
    String year = request.getParameter("year");
    String address = request.getParameter("Adress");
    db.registerUser(fName, lName, email, username, password, gender, month, day, year, address);
    session.setAttribute("login_status", "connected");
}
db.close();
%>
<jsp:forward page="index.jsp" />

DB_something is a class which open and close the connection with database and check if the sign in was ok and also register the user in case of sign up

index.jsp

    <%
    if (request.getParameter("login_status") == null) {
    %>
    <jsp:include page="login.html" />
    <%
    } else {
        if(1==1)
            throw new Exception("error...");
    %>
    <jsp:include page="connected.jsp" />
    <%
    } 
    %> .  .  .  .

in my index.jsp i tried to include the login.html if the user is not connected(login_status = null) and include the connected.jsp if the user is connected(login_status = "connected") the problem is that it is not working.it is always adding the login.html..I even try add throw exception(the if 1==1 is because otherwise exception was thrown..) but the output is always the same(the exception is never working) Any ideas?


Solution

  • You save the attribute in the session, but trying to extract it from the request.

    Instead of the directive -

    <jsp:forward page="index.jsp" />
    

    use this:

    <jsp:forward page="index.jsp">
        <jsp:param name="login_status" value="connected" />
    </jsp:forward>
    

    Or extract the attribute from the session:

    if(request.getSession().getAttribute("login_status") == null) {
       ...
    }