Java Web Servlet Learning

Source: Internet
Author: User

At the beginning of Java Web Servlet learning, Let's first look at what Java Web contains? A complete Java Web is usually composed of the presentation layer, control layer, business layer, and data access layer. the presentation layer is usually built by HTML and JSP pages. The control layer is generally Servlet, the business logic layer is JavaBean or EJB, and the data access layer component is JDBC or Hibernate. java Web Components in the configuration file (. xml), and then package it and deploy it to the Java Web Server (Tomcat ...).

Java Web Servlet is a server-side program written in java. It is a Java class and has the advantages of portability and scalability. through javax. servlet and javax. servlet. the http package is supported by Java. especially javax. servlet. the http packet provides methods for the HTTP protocol, including processing HTTP requests and responses (HttpServlet class), which are processed by doGet () and doPost () respectively.

Java code

 
  1. protected void doGet(HttpServletRequest request,HttpServletResponse response)  
  2. throws ServletException,java.io.IOException;    
  3. protected void doPost(HttpServletRequest request,HttpServletResponse response)  
  4. throws ServletException,java.io.IOException;   

Processing of customer requests (HttpServletRequest class)

Java code

 
  1. HttpServletRequest request;    
  2. String name = request.getParameter("username");   

Send the request to the customer in HTML format (HttpServletResponse class)

Record user sessions in the current Servlet (HttpSession class)

Java code

 
  1. HttpServletRequest request;  
  2. HttpSession session = request.getSession(true); 

Submit or delegate the Servlet request to another resource (RequestDispatcher class). The RequestDispatcher points to the URL of a specific resource and can be generated using the getRequestDispatcher () method of the HttpServletRequest object.

Java code

 
  1. RequestDispatcher rd = request.getRequestDispatcher("WELCOME.JSP");  
  2. rd.forward(request,response); 

In Java Web, servlets are usually used as controller components and must be declared in the configuration file (web. xml.

Java code

Web. xml

 
  1. ...    
  2. ﹤servlet﹥    
  3. ﹤servlet-name﹥myServlet﹤/servler-name﹥    
  4. ﹤servlet-class﹥myServlet﹤/servlet-class﹥    
  5. ﹤/servlet﹥    
  6. ﹤servlet-mapping﹥    
  7. ﹤servlet-name﹥myServlet﹤/servlet-name﹥    
  8. ﹤url-pattern﹥/myServlet﹤/url-pattern﹥    
  9. ﹤/servlet-mapping﹥    
  10. ...   

The learning of Java Web Servlet is to be interspersed and consolidated. I hope this article will be helpful to you.

  1. At the beginning of JSP Servlet Development
  2. Test Servlet configuration and development
  3. Select JSP development tools
  4. Servlet and JSP paths
  5. What are servlets and common Servlet APIs?

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.