Servlet learning, servlet

Source: Internet
Author: User

Servlet learning, servlet

The Learning Website is from www.imooc.com.

Servlet Basics
1. What is Servelet?
Servelet is a small program running on the server. A Servlet is a Java class.
And can be accessed through the "request-response" programming model. This resident in the server memory
Servlet program. Servlet is the predecessor of jsp.

2. Tomcat container level
Tomcat containers are divided into four levels: Servlet container management Context container, one
Context corresponds to a Web project. Engine is an Engine container; Host is a Host container.

 

3. Compile a Servlet
There are three steps.
1. Compile a class to inherit HttpServlet
2. Rewrite the doGet () or doPost () method. It depends on the submission method.
3. Register Servlet.
The procedure is as follows:

Code in index. jsp:

<Body> 

Code in HelloServlet. java:

Package Servlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; // inherit from HttpServletpublic class HelloServlet extends HttpServlet {// rewrite method @ Overrideprotected void doGet (HttpServletRequest request, HttpServletResponse response) throws S ErvletException, IOException {// TODO Auto-generated method stubSystem. out. println ("processing Get () requests... "); // get an out object output to the browser, type: PrintWriterPrintWriter out = response. getWriter (); // specify the type of the output file response. setContentType ("text/html; charset = UTF-8"); // use the out object to output a message to the browser. You can use the html code out. println ("<strong> Hello Servelet! </Strong> <br> "); // 3. register; web in WEB-INF. register in xml} @ Overrideprotected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubSystem. out. println ("processing post () requests... "); // get an out object output to the browser, type: PrintWriterPrintWriter out = response. getWriter (); // specify the type of the output file response. setContentType ("text/html; charset = UTF-8"); // use the out object to output a message to the browser. You can use the html code out. println ("<strong> Hello Servelet! Post </strong> <br> "); // 3. Register; register in web. xml in the WEB-INF; do not repeat after registration }}

Registration Code in web. xml:

The registration process is to add two tags in web. xml.
<Servlet> </serclet> and <servlet-mapping> </servlet-mapping>
Each tag must contain two sub-tags.
In <servlet> </servlet>, you must include <servlet-name> to give the Servlet a name.
</Servlet> and <servlet-calss> the Class Name of the Servlet to be accessed (package name + class name)
</Servlet-class>
In <servlet-mapping> </servlet-mapping>
Get a Servlet name (Note: The name must be the same as above) </servlet> and <urlpattern>
Servlet access path </url-pattern>

<servlet>  <servlet-name>HelloServlet</servlet-name>  <servlet-class>Servlet.HelloServlet</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name>HelloServlet</servlet-name>  <url-pattern>/servlet/HelloServlet</url-pattern>  </servlet-mapping>  

 

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.