first, the bottom of the servlet introduction
Servlet is running on the Web server side of the Java program, can generate dynamic Web pages, belong to the customer and server corresponding to the middle tier, so it can be said that JSP is a servlet, the two can achieve the same page effect, for JSP and servlet code writing, The former is easier, but the servlet is not useless, he can help the programmer to understand the underlying details, in addition, the servlet is a Java class, suitable for pure programming.
second, how to create a servlet
The steps are as follows:
Pause: The function we want to implement is to display a piece of text using a servlet, and the work to do is to create a servlet
1. Create a Java Web project first, as shown in the following illustration:
Figure 1 new Web project
We then create the servlet class in the source package by right-clicking the source package, selecting new "Servlet class", then entering the new Class dialog box, filling in the new servlet class, selecting the package location in the package location, and clicking Next, Select to deploy the information to Web.xml, as shown in the following illustration:
(1) Enter to establish new servlet interface
Figure 2 New Servlet dialog box
(2) Deploy Web.xml configuration
Figure 3 Deploying servlet information into Web.xml
Why to tick the web.xml before the selection box, is to tell the system this kind of, check, we can see the following code in the Web.xml file, that is, if there is no check, you can in the Web.xml by adding code form to deploy, the code is as follows:
<servlet>
<servlet-name>ShowServlet</servlet-name>
<servlet-class>servlet. showservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> showservlet</servlet-name>
<url-pattern>/ShowServlet</url-pattern>
</ Servlet-mapping>
(3) The component completes the servlet
Figure 4 the new servlet class
2, after the establishment, we have a servlet class to write
When it comes to the servlet, since it is a class, why is it so different, mainly because the so-called servlet contains such a sentence
public class Showservlet extends HttpServlet {//This is the difference between the servlet class and others, inheriting the HttpServlet class
Here is the modified code:
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; public class Showservlet extends HttpServlet {//This is the difference between the servlet classes and others, inheriting the httpservlet protected void ProcessRequest (Http ServletRequest request, HttpServletResponse response) throws Servletexception, IOException {response.
setContentType ("Text/html;charset=utf-8"); Try (PrintWriter out = Response.getwriter ()) {/* TODO output your page here. Use following sample code. * * OUT.PRINTLN ("<!
DOCTYPE html> ");
Out.println ("You can then right-click on the Servlet (or enter http://localhost:8080/Demo_09_Servlet/ShowServlet in the browser URL) to see the results shown below:
Figure 5 The real-world effect of the servlet
The above is the process of establishing the servlet.