JSP + servlet instance

Source: Internet
Author: User

Development Environment: myeclipse + atat6

1. Create a web project named registersystem,

2. Create the login. jsp file in the webroot directory and modify the content in 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. Compile the acountbean. Java file in COM. Ht. servlet under the SCR directory,CodeAs follows:

Package com. Ht. servlet;

Public classAccountbean{
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. Compile the servlet checkaccount. Java file in COM. Ht. servlet under the SCR directory. The Code is as follows:

Package com. Ht. servlet;

Import java. Io. ioexception;

Import javax. 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 extends httpservlet {

@ Override
Protected void dopost (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Doget (req, resp );
}

@ Override
Public void doget (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Httpsession session = Req. getsession ();
Accountbean account = new accountbean ();
String username = 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 );
String login_suc = "success. jsp ";
Resp. sendredirect (login_suc );
Return;
}
}
String login_fail = "fail. jsp ";
Resp. sendredirect (login_fail );
Return;
}

}
5. After the success. jsp file is successfully written in the webroot directory, jump

<Body>
<%
Accountbean account = (accountbean) Session. getattribute ("account ");
%>
Username: <% =Account. GetUserName () %>
<Br>
Password: <% =Account. GetPassword ()%>
</Body>

6. Jump to the webroot directory after writing the fail. jsp file fails.

<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 ">

login. JSP


This is the description of my J2EE component
This is the display name of my J2EE component
checkaccount
COM. ht. servlet. checkaccount

<Servlet-mapping>
<Servlet-Name>Checkaccount</Servlet-Name>
<URL-pattern>/Login</Url-pattern>
</Servlet-mapping>

 
</Web-app>

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.