Java Learning (vi) Introduction of Servlets

Source: Internet
Author: User

First, Servlet1, understanding servlet

Servlet (Server Applet), full name Java Servlet, without Chinese translation. is a server-side program written in Java. Its main function is to interactively browse and modify data to generate dynamic Web content. The narrow-sense servlet refers to the Java language implementation of an interface, the generalized servlet refers to any implementation of the Servlet interface class, in general, people will understand the servlet as the latter. The servlet runs in a Java-enabled application server. From the implementation, the servlet can respond to any type of request, but in most cases the servlet is used only to extend the HTTP protocol-based Web server. The first to support the servlet standard is JavaSoft java Web Server. Since then, some other Java-based Web servers have started to support standard Servlets. Currently the latest version is 3.0.

2. Realization Process

The main function of a servlet is to interactively browse and modify data to generate dynamic Web content. This process is:

The client sends the request to the server side;

The server sends the request information to the servlet;

The servlet generates and passes the response content to the server. Response content is generated dynamically, usually depending on the client's request;

The server returns the response to the client.

Ii. introduction of Servlets

Analyze the previous example code to get: The pattern that the code applies To

There is a problem with this mode:

1, JSP need to call JavaBean

  Studentservice Stuservice = new Studentserviceimpl ();

  list<studentinfo> stulist = stuservice.getallstudents ();

We look forward to removing the 2 lines of code to make the JSP page more concise

2, lack of controller (jump switch)

This is the post-modification pattern: MVC mode

Iii. Example 1, establishing an entity class

Refer to the two-dimensional table, consider each field name, type

1  Public classHouse {2     PrivateString ID;3     PrivateString name;4     PrivateString address;5      PublicString getId () {6         returnID;7     }8...//other
2. Establish business logic Interface (HOUSESERVICE) and implementation (HOUSESERVICEIMPL)

Interface:

1  Public Interface Houseservice {2      Public List getallhouses (); 3 }

Implementation class:

1 @Override2      PublicListgetallhouses () {3ListNewArraylist();4         //1 Bald-headed home forest, number 188th.5         //2 Bear Big House forest tree hole Phase 3 floor6House H1 =NewHouse ();7House H2 =NewHouse ();8         9H1.setid ("1");TenH1.setname ("Home with a bald head"); OneH1.setaddress ("Forest No. 188th"); A          -H2.setid ("2"); -H2.setname ("The Big Bear's house")); theH2.setaddress (3/F, Forest Tree Cave)); -          -          - Houselist.add (H1); + Houselist.add (H2); -          +         returnhouselist; A}
3. Create a (Servlet) controller, call the service to get a List of datasets . "Send the results to the JSP and jump to the past" 3.1. Create an Action package

3.2. Create a servlet

To create a new servlet in the action package:

3.3. Fill in the test code

The above is the test jump code

1 request.setcharacterencoding ("UTF-8"); 2         3         response.setcharacterencoding ("UTF-8"); 4         Response.setcontenttype ("Text/html;charset=utf-8;pageencoding=utf-8"); 5         6         printwriter out =  Response.getwriter (); 7         out.println ("Hello Shen expands registered ありませетийдоктор");   

Above here output specific HTML code, note: This is a counter example, should not output HTML content in the servlet, the servlet should do the function of the controller.

3.5. Set up a show.jsp to get the list in the Request object

Core code: Request.getattribute ("Houselist_label");

Today's study to this report a paragraph, tomorrow will continue to study and update the article.

Java Learning (vi) Introduction of Servlets

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.