Learn the servlet (ii) servlet basics

Source: Internet
Author: User

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

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.