Now the framework is more popular, in order to better understand the framework, Javaweb Basic development form also need to understand.
The following is the application of Jsp+servlet technology to implement login. (Database is not used, data operation is relatively simple, this example mainly describes the development process)
Project:jspservletlogintest
The directory structure of the project is as follows:
The following code is attached:
Index.html
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > <!-- The login page is a simple login interface-->
login_success.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Loginservlet.java
Package test.login;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Loginservlet extends HttpServlet {private static final long serialversionuid = 1L;
Public Loginservlet () {super ();
} protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
{Response. setContentType ("text/html");
Get username String susername = request.getparameter ("txtUserName");
Get password String spasswd = Request.getparameter ("Txtpassword");
Test System.out.println ("Username:" +susername+ "\ r \ n" + "Password:" +spasswd); Request.
GetSession (). setattribute ("UserName", sUserName); Request.getsession (). setattribute ("password", spasswd);
Just compare with Hym&123,not do database operation if (Susername.equals ("Hym") &&spasswd.equals ("123")
{Response. Sendredirect ("login_success.jsp");
else {response. Sendredirect ("login_failure.jsp");
}
}
}
So far, the main functional files for the project have been completed. However, you will also register the welcome file that the project runs and the registration of the servlet in Web.xml. (about the working principle of the Web.xml file has the opportunity to fill in)
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app id= "webapp_id" 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/ Java http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <display-name>jspservletlogintest</ display-name> <servlet> <description> </description> <display-name>loginservlet</dis play-name> <servlet-name>loginServlet</servlet-name> <servlet-class> Test.login.loginServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>loginservlet</servlet-name > <url-pattern>/loginServlet</url-pattern> </servlet-mapping> <!--setting up the Web with the following
When project runs on a tomcat server, you can first start and run the corresponding page under the WebContent directory, and retrieve--> <welcome-file-list> according to Welcome-file's list. <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welCome-file> </welcome-file-list> </web-app>
This enables the user login feature implemented by Jsp+servlet.