Technique: Implementation of JSP login verification function

Source: Internet
Author: User
Tags include tomcat server
js| Tips

Static login interface design login.htm, the code is as follows:

<title> System Login </title>
<style type= "Text/css" ....
<!--
. Style1 {...} {
font-size:18px;
Font-weight:bold;
}
. Style2 {...} {FONT-SIZE:24PX}
. style5 {...} {FONT-SIZE:16PX}
-->
</style>
<body bgcolor= "papayawhip" width= "height=" >
<center>
<table border= "2" bordercolor= "Black" bgcolor= "LightGreen" >
<tbody>
<tr>
<td><div align= "center" class= "Style1 style2" > System Login
</div></td>
</tr>
<form action= "login.jsp" method= "POST" >
<tr>
&LT;TD height= ><span class= "style5" > User name </span> <input type= "text" name= "UID" maxlength= "2 0 "style=" width:150 "></td></tr><br>
<tr>
<td><span class= "style5" > Password </span> <input type= "password" name= "upwd" maxlength= "style=" width:150 "></td></tr><br>
<center>
<tr><td><div align= "center" >
<input type= "Submit" value= "Login" >
<input type= "reset" value= "Cancel" >
</div></td></tr>
</center>
</form>

</tbody>
</table>
</center>
</body>

To submit the information entered by the Logged-in user to the LOGIN.JSP page model processing, here for convenience, do not perform the database access operation, directly using sky2098 as the login username and password, but in practice is to read from the database, the JSP page code implementation is as follows:

<%...@ page contenttype= "text/html;charset=gb2312"%>
<% ...
if (Request.getparameter ("UID"). Equals ("sky2098") &&request.getparameter ("Upwd"). Equals ("sky2098")) {
Session.setattribute ("Login", "OK");
Session.setmaxinactiveinterval (-1);
%>
<jsp:forward page= "main.jsp"/>
<% ...
}else{
Out.println ("User name or password input error!") ");
}
%>

If the login is successful, then set login value is OK, submit to the next verification page, then enter the main.jsp page, otherwise, if you enter the username and password is not legitimate to print error messages, main.jsp page code as follows:

<%...@ page contenttype= "text/html;charset=gb2312"%>
<%...@ include file= "checkvalid.jsp"%>
<title>~welcome to my homepage~</title>
<body>
<center>
~welcome to my homepage~
</center>
</body>

This page uses the <% @ include file= "checkvalid.jsp"%> contains a JSP page checkvalid.jsp to verify the legality of the input information:

<% ...
if (Session.getattribute ("login") ==null| |! Session.getattribute ("Login"). Equals ("OK")) {
Response.sendredirect ("login.htm");
}
%>

If the input information is incorrect, return to the login page and re-enter the login information.

Test the login feature.

Start the Tomcat server and type the URL in the IE Address bar as:

Http://localhost:8080/sky2098/login-Advanced/login.htm



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.