Create a simple login page using jsp and session

I created a simple login page in which the user will provide a username and password, and then it will be saved in the session. After clicking the "Submit" button, it will display a welcome user or name. And if the user waits a few seconds, the session will expire and he will automatically return to the login page.

Here is my login page

<%@ page import="java.io.*,java.util.*" 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"> <jsp:useBean id="user" class="user.LoginUser" scope="session"></jsp:useBean> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>login</title> </head> <body> <h1><center>Give your login details</center></h1> <form method="post" action="check.jsp"> Username:<input type="text" name="username" size="20" value="<%=user.getUser() %>" > <br> Password:<input type="password" name="password" size="20" value=<%=user.getPassword() %> ><br> <input type="submit"> </form> </body> </html> 

now in check.jsp i do my control part for username and password

 <%@ page import="java.io.*,java.util.*" 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"> <jsp:useBean id="user" class="user.LoginUser" scope="session"></jsp:useBean> <jsp:setProperty name="user" property="*"/> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>login checking</title> </head> <body> <% String USER=user.getUsername(); int PASSWORD=user.getPassword(); if(USER.equals("abhirup")) { if(PASSWORD==54321) { pageContext.forward("display.jsp"); } else { out.println("Wrong password"); pageContext.include("login.jsp"); } pageContext.include("login.jsp"); } %> </body> </html> 

and then finally i show it in display.jsp

 <%@ page import="java.io.*,java.util.*" 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"> <jsp:useBean id="user" class="user.LoginUser" scope="session" ></jsp:useBean> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Display</title> </head> <body> <% String title="Welcome : successful login"; out.println(title);%> <h3><center>Your Name:Abhirup Parui</center></h3><br> Username<%=user.getUsername()%><br> <%session.setMaxInactiveInterval(20); pageContext.include("login.jsp"); %> </body> </html> 

and also this is my LoginUser.java file

 package user; public class LoginUser { String username; int password; public void setUsername(String value) { username=value; } public void setPassword(int value) { password=value; } public String getUsername(){return username;} public int getPassword(){return password;} } 

I am using the Eclipse IDE and the Tomcat server. Eclipse did not show a single error on any page, but still, when I launched my login.jsp page.

I get this error when starting login.jsp

I followed this link

please help me find my mistakes.

Update

I can successfully launch my login page. I am getting this error now, but cannot figure out where the error is. the last part of the error is

how to fix this error. help

+6
source share
2 answers

Since you are trying to access login.jsp directly from the browser, you need to move it from the WEB-INF directory - the files in WEB-INF are not accessible to the public. If you move login.jsp to the same directory and enter http://localhost:8088/abhirup/login.jsp into your browser, it should pull the page. However, it is quite common practice to put jsp pages in WEB-INF / jsp or something similar and use a servlet to intercept and process requests, and then pass the servlet to the corresponding jsp page.

You have a syntax error in row 1, column 46 of display.jsp, because before the declaration of language you have the word page . Change this:

 <%@ page import="java.io.*,java.util.*" page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 

:

 <%@ page import="java.io.*,java.util.*" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
+4
source

I also tried the same code and I found an error in two JSP files

The corrected login.jsp code is shown below:

 <%@ page import="java.io.*,java.util.*" language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="user.LoginUser"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <jsp:useBean id="user" class="user.LoginUser" scope="session"></jsp:useBean> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login page</title> </head> <body> <h1><center>Give your login details</center></h1> <form method="post" action="check.jsp"> User name:<input type="text" name="username" size="20" value="<%=user.getUsername() %>"><br> Password:<input type="password" name="password" size="20" value="<%=user.getPassword()%>" ><br> Submit <input type="submit"> </form> </body> </html> 

The corrected check.jsp code is as follows:

 <%@ page import="java.io.*,java.util.*" language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <%@ page import="user.LoginUser"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <jsp:useBean id="user" class="user.LoginUser" scope="session"></jsp:useBean> <jsp:setProperty name="user" property="*"/> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login checking</title> </head> <body> <% String USER=user.getUsername(); String PASSWORD=user.getPassword(); if(USER.equals("admin")) { if(PASSWORD.equals("admin")) { pageContext.forward("display.jsp"); } else { out.println("Wrong password"); pageContext.include("login.jsp"); } pageContext.include("login.jsp"); } %> </body> </html> 

Fixed display.jsp code:

 <%@ page import="java.io.*,java.util.*" language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <%@ page import="user.LoginUser"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <jsp:useBean id="user" class="user.LoginUser" scope="session" ></jsp:useBean> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Display</title> </head> <body> <% String title="Welcome : Successful Login"; out.println(title);%> <h3> <center> Your Name : Reneesh </center> </h3><br> User name : <%=user.getUsername()%><br> <%session.setMaxInactiveInterval(20); %> </body> </html> 

My file with Java code LoginUser.java looks like this:

 package user; public class LoginUser { String username; String password; public void setUsername(String value) { username=value; } public void setPassword(String value) { password=value; } public String getUsername() { return username; } public String getPassword() { return password; } } 

Please try using this code, I made some changes to the code by assigning a String value to the password. I also used the Eclipse juno IDE and Apache Tom Cat v 7.0 to run this dynamic web project. Hope you try and let me know if there is another mistake.

+1
source

Source: https://habr.com/ru/post/951661/


All Articles