A simple Jsp+servlet instance for simple login
reprint http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html
Development environment MYECLIPSE+TOMCAT6
1. Create Web project first, project name is Registersystem,
2. To create the login.jsp file in the Webroot directory, simply modify the contents of the body as follows:
<body>
<form action= "Login" >
Username:<input type= "text" name= "username" ><br>
Password:<input type= "password" name= "pwd" ><br>
<input type= "Submit" >
</form>
</body>
3, in the SCR directory Com.ht.servlet write Acountbean.java file, the code is as follows:
Package com.ht.servlet;
public class Accountbean {
Private String username = "";
Private String Password = "";
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
}
4, in the SCR directory Com.ht.servlet writing servlet class Checkaccount.java file, the code is as follows:
Package com.ht.servlet;
Importjava.io.IOException;
Importjavax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
public class CheckAccount Extendshttpservlet {
@Override
protected void DoPost (HttpServletRequest req,httpservletresponse resp)
Throwsservletexception, IOException {
doget (REQ,RESP);
}
@Override
public void doget (HttpServletRequest req,httpservletresponse resp)
Throwsservletexception, IOException {
HttpSession session =req.getsession ();
Accountbeanaccount = Newaccountbean ();
stringusername = Req.getparameter ("username");
String pwd =req.getparameter ("pwd");
Account.setpassword (PWD);
Account.setusername (username);
if ((username!=null) && (Username.trim (). Equals ("JSP"))) {
if (pwd!=null) && (Pwd.trim () Equals ("1"))) {
SYSTEM.OUT.PRINTLN ("Success");
Session.setattribute ("account", account);
STRINGLOGIN_SUC = "success.jsp";
Resp.sendredirect (LOGIN_SUC);
Return
}
}
String login_fail = "fail.jsp";
Resp.sendredirect (Login_fail);
Return
}
}
5, in the Webroot directory to write the success.jsp file after successful jump
<body>
<%
Accountbean account = (Accountbean) Session.getattribute (the "account");
%>
username:<%= account.getusername ()%>
<br>
password:<%= Account.getpassword ()%>
</body>
6, in the Webroot directory to write fail.jsp file failure after the jump
<body>
Login failed!<br>
</body>
7. Modify the Web. XML configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>this is the descriptionof my j2eecomponent</description>
<display-name>this is the displayname of my j2eecomponent</display-name>
<servlet-name>CheckAccount</servlet-name>
<servlet-class>com.ht.servlet.CheckAccount</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckAccount</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
&NBSP;
</web-app>