Java Learning (6) Introduction of servlet, assumervlet

Source: Internet
Author: User

Java Learning (6) Introduction of servlet, assumervlet
I. Servlet1. Learn about Servlet

Servlet (Server Applet), full name of Java Servlet, no Chinese translation. It is a server-side program written in Java. Its main function is to interactively browse and modify data to generate dynamic Web content. Servlet in the narrow sense refers to an interface implemented by the Java language. Servlet in the broad sense refers to any class that implements this Servlet interface. In general, people think of Servlet as the latter. Servlet runs on an application server that supports Java. In terms of implementation, Servlet can respond to any type of requests, but in most cases, Servlet is only used to expand the HTTP-based Web server. The earliest Java Web Server that supports Servlet standards is the slave oft. Later, some other Java-based Web servers began to support standard servlets. The latest version is 3.0.

2. Implementation Process

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

The client sends a request to the server;

The server sends the request information to the Servlet;

The Servlet generates the response content and sends it to the server. The dynamic generation of response content usually depends on the client request;

The server returns the response to the client.

Ii. Introduce Servlet

Analyze the previous example code to find that the Code applies the correct mode.

This mode has a problem:

1. jsp needs to call javabean

StudentService stuService =NewStudentServiceImpl ();

List <StudentInfo> stuList = stuService. getAllStudents ();

We expect to remove these two lines of code to make the jsp page more concise.

2. Lack of controller (for redirection)

This is the modified mode: MVC Mode

3. Example 1: Create an object class

Refer to the "tables" table to consider the names and types of each field.

1 public class House {2 private String id; 3 private String name; 4 private String address; 5 public String getId () {6 return id; 7} 8... // Others
2. Establish the HouseService and implementation (HouseServiceImpl)

Interface:

1 public interface HouseService {2     public List<House> getAllHouses();3 }

Implementation class:

1 @ Override 2 public List <House> getAllHouses () {3 List <House> houseList = new ArrayList <House> (); 4 // 1 bald-headed home forest No. 188 5 // 2 xiongda home forest tree cave Phase I 3 floor 6 House h1 = new House (); 7 House h2 = new House (); 8 9 h1.setId ("1"); 10 h1.setName (""); 11 h1.setAddress ("Forest 188 "); 12 13 h2.setId ("2"); 14 h2.setName ("Home of xiongda"); 15 h2.setAddress ("Third floor, phase I, forest tree Cave"); 16 17 18 houseList. add (h1); 19 houseList. add (h2); 20 21 return houseList; 22}
3. Create a Servlet controller and call service to obtain the dataset List. [Deliver the result to jsp and jump to the past] 3.1. Create an action package

3.2 Create a Servlet

Create a Servlet in the action package:

 

3.3 enter 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 ");

The specific html code is output here. Note: This is a counterexample. html content should not be output in the Servlet. Servlet should be used as the controller function.

3.5 create a show. jsp file to obtain the List in the request object

Core code: request. getAttribute ("houseList_label ");

 

Today's learning comes to an end. I will continue to learn and update the article tomorrow.

 

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.