Development Environment: myeclipse + atat5
Create a web project named test,
Create a login. jsp file in the webroot directory
Login. jsp
<Body>
<Form action = "login">
Username: <input type = "text" name = "username"> <br>
Password: <input type = "password" name = "PWD"> <br>
<Input type = "Submit">
</Form>
</Body>
Com. Ht. servlet in the SCR directory
Compile the acountbean. Java File
Package com. Ht. servlet;
Public class acountbean {
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;
}
}
Compile the servlet checkaccount. Java File
Package com. Ht. servlet;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http .*;
Public class checkaccount extends httpservlet {
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Httpsession session = request. getsession ();
Acountbean account = new acountbean ();
String username = request. getparameter ("username ");
String Pwd = request. getparameter ("PWD ");
Account. setusername (username );
Account. setpassword (PWD );
If (username! = NULL) & (username. Trim (). Equals ("jsp "))){
If (PWD! = NULL) & (PWD. Trim (). Equals ("1 "))){
System. Out. println ("session ");
Session. setattribute ("Account", account );
String logon_suc = "session. jsp ";
Response. sendredirect (logon_suc );
Return;
}
}
String logon_fail = "fail. jsp ";
Response. sendredirect (logon_fail );
Return;
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}
Under the webroot directory
Jump to the session. jsp file after successful compilation
<Body>
<%
Com. Ht. servlet. acountbean account = (COM. Ht. servlet. acountbean) Session. getattribute ("account ");
%>
Username: <% = Account. GetUserName () %>
<Br>
Password: <% = Account. GetPassword () %>
</Body>
Jump after failed to write fail. jsp file
<Body>
Logon Failed <br>
</Body>
Below is the Web. xml file
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
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>
<Welcome-file-List> <welcome-File> login. jsp </welcome-File> </welcome-file-List>
<Servlet>
<Description> This is the description of my J2EE component </description>
<Display-Name> This is the display name of my J2EE component </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>
</Web-app>
Running and configuring Tomcat through myeclipse is omitted here...
Http: // 127.0.0.1: 8080/tset/verify and view the result
Source code download http://tel4.800disk.com /? Mbsky0618 password mbsky0618