Simple login example of pure JSP implementation
- 2021-11-10 10:28:54
- OfStack
This paper describes the simple login method realized by pure JSP. Share it for your reference, as follows:
There are 4 files web. xml, login. jsp, logout. jsp, welcome. jsp
Test environment: Tomcat 6.0. x
Assuming the project name is LoginSample, my directory structure is like this
...\webapps\LoginSample\WEB-INF\web.xml
...\webapps\LoginSample\login.jsp
...\webapps\LoginSample\logout.jsp
...\webapps\LoginSample\welcome.jsp
web. xml source list:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
login. jsp source list:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>JSP Simple login instance </title>
</head>
<body>
<h2> Please log in </h2>
<form method="POST" >
Login Name: <input type="text" name="Name"><br>
Login Password: <input type="text" name="Password" ><br>
<input type="submit" value="Send"><br>
<form>
<%
if (request.getParameter("Name") != null
&& request.getParameter("Password") != null) {
String Name = request.getParameter("Name");
String Password = request.getParameter("Password");
if (Name.equals("a") && Password.equals("a")) {
session.setAttribute("Login", "OK");
session.setAttribute("myCount", new Integer(1));
response.sendRedirect("welcome.jsp");
}
else {
%>
Login failed : Incorrect username or password ~
<%
}
}
%>
</body>
</html>
logout. jsp source list:
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<%
session.setAttribute("Login", "");
%>
<body>
<h2> You have logged out </h2>
</body>
</html>
welcome. jsp source list:
<%@ page contentType="text/html" pageEncoding="UTF-8" import="java.util.*"%>
<html>
<body>
<h2> Welcome page ( Test session)</h2>
<%
String Login = (String)session.getAttribute("Login");
int nCount=0;
if (Login != null && Login.equals("OK")) {
Integer myCount = (Integer)session.getAttribute("myCount");
if(myCount!=null)
{
nCount = myCount.intValue();
nCount = nCount + 1;
session.setAttribute("myCount",new Integer(nCount));
}
%>
Login Successful ,myCount=<%=nCount%></br>
<input type=button value=" Quit " onclick="javascript:location.href='logout.jsp'">
<%
}
else {
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
</body>
</html>
I hope this article is helpful to everyone's JSP programming.