1, we try to submit a form to a servlet, now webcontent a new login.html page, where action corresponds to the Servelt class name, the code is as follows:
<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"><title>Submit Form</title></Head><Body><formAction= "Loginservlet"Method= "POST">User name:<inputname= "username"type= "text"><BR>Password:<inputname= "Password"type= "Password"><BR><inputvalue= "Submit"name= "Submit"type= "Submit"></form></Body></HTML>
Code
2, create a new servlet class Loginservlet, the code is as follows:
PackageServletdemo;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class Loginservlet*/@WebServlet ("/loginservlet") Public classLoginservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /** * @seeHttpservlet#httpservlet ()*/ PublicLoginservlet () {System.out.print ("Loginservlet ..."); //TODO auto-generated Constructor stub} @Override Public voidInit ()throwsservletexception {//TODO Auto-generated method stubsSystem.out.print ("Init ..."); } /** * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/ protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubSystem.out.print ("Doget ..."); } /** * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubSystem.out.print ("Dopost ..."); } @Override Public voiddestroy () {//TODO Auto-generated method stubsSystem.out.print ("Destory ..."); }}Code
Note: right-clicking a new servlet does not generate a corresponding configuration tag in Web. XML because the Servlet class already has @WebServlet annotations
3, run login.html, can verify the life cycle of the servlet, the effect is as follows:
4, sometimes need to get some configuration information in Web. XML, such as the following configuration, where name is equivalent to Key,value is the corresponding value
<context-param>
<param-name>henry</param-name>
<param-value>123</param-value>
</context-param>
With this line of code you can get the value corresponding to the key:String result=this.getservletcontext (). Getinitparameter ("Henry");
5, servlet in MVC equivalent controller, so often need to do some page jump, below to see the implementation of page navigation
Learn the servlet (ii) servlet basics