This paper mainly involves two contents:

  1. Using MyEclipse simplifies the process of creating servlets
  2. The difference between GET and POST requests

Haha, after listing these two items, Cat feels full of dry goods.

The first is 1. Using MyEclipse simplifies the process of creating servlets, because writing a Servlet by hand, doing the same web.xml configuration file modification every time, inheriting HttpServlet methods and overwriting doGet and so on, is boring. Mocat said that the most basic thing about programming is to avoid repetition, which is boring and cumbersome to maintain. The MyEclipse developers designed tools to make it easier for us to write servlets without having to repeat ourselves.

To do this, right-click package [servlet] and select [New] – [servlet]. The diagram below:

See here, there is a refreshing feeling, are automatically generated ah

FirstAutoServlet (PS) superclass: FirstAutoServlet (PS) Javax.mail. Servlet. HTTP HttpServlet, said inherit this class doGet () : the doPost method of dealing with a get request () : to deal with the post request methodCopy the code

OK, that’s all you need to know, click next, as shown below:



The explanation is as follows:

Generate/Map web. XML file Generate/Map web. XML file Generate/Map web. XML file Generate/Map web. XML file Generate/Map web. XML file Generate/Map web. This is the matching user request style FIle Path of web. XML: this does not explain the DisPlay Name and Description, which are equivalent to comments, SO I delete themCopy the code

Click Finish, the project has changed, and two tabs will automatically open in our code editing area to indicate that the two changes have taken place. Let’s take a look:

One is to automatically generate this web. XML configuration (the old one, MyTimeServletName, has been removed so that it looks clear) :

 <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>FirstAutoServlet</servlet-name>
    <servlet-class>servlet.FirstAutoServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FirstAutoServlet</servlet-name>
    <url-pattern>/servlet/FirstAutoServlet</url-pattern>
  </servlet-mapping>
Copy the code

These are generated automatically for our FirstAutoServlet. They are not concise enough to suit the taste of Moggy.

  <servlet>
    <servlet-name>FirstAutoServlet</servlet-name>
    <servlet-class>servlet.FirstAutoServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FirstAutoServlet</servlet-name>
    <url-pattern>/FirstAutoServlet</url-pattern>
  </servlet-mapping>
Copy the code

OK, with the configuration above, when the user accesses the /FirstAutoServlet request under the ServletDemo project, the system calls servlet.firstAutoServlet. Let’s open the autogenerated FirstAutoServlet class as follows:

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 FirstAutoServlet extends HttpServlet {
	public FirstAutoServlet(a) {// constructor
		super(a); }public void destroy(a) {// The destruct function is automatically called when FirstAutoServlet is destroyed.
		super.destroy();
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {// Be responsible for responding to get requests
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("
      );
		out.println("<HTML>");
		out.println(" A Servlet");
		out.println(" ");
		out.print(" This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println(" ");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {// Responsible for responding to POST requests
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("
      );
		out.println("<HTML>");
		out.println(" A Servlet");
		out.println(" ");
		out.print(" This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println(" ");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
	public void init(a) throws ServletException {// FirstAutoServlet initialization function is called when FirstAutoServlet is initialized}}Copy the code

When we look at this class, we know, “Oh My God,” that’s how a standard Servlet should be written. Now let’s focus on the doGet and doPost methods, noting that the output of the two methods is different. First to redeploy and then restart the Tomcat (note: editing the Servlet must redeploy and then restart the Tomcat to come into force, remember?), and then in the browser address bar to visit: http://127.0.0.1:8080/ServletDemo/FirstAutoServlet. Web page output This is class servlet FirstAutoServlet, using the GET method, said transferred to our doGet () method. OK, go on, if we create a visitFirstAutoServlet the following web pages. HTML.

<! -- visitFirstAutoServlet.html -->
<! DOCTYPEhtml>
<html>
  <head>
    <title>visitFirstAutoServlet.html</title>
  </head>
  <body>
    <a href="http://127.0.0.1:8080/ServletDemo/FirstAutoServlet">Click here to access FirstAutoServlet</a><br>
  </body>
</html>
Copy the code

Try clicking again from the web pageClick here to visitLink, as shown below:



After clicking the link, the page displays:This is class servlet.FirstAutoServlet, using the GET method. As you can see, access via hyperlink is still get.

Post: Get: Post: get: POST: Get: POST Basically, get is used to request content and POST is used to submit parameters for changes and updates. Since GET can also submit parameters, and POST can also get content, there’s not much difference. From a programmer’s point of view, it is better to use POST because you can pass more parameters and be more secure. From this description, it can be found that post is mainly for passing parameters, that is, from one side of the web page to the backend Servlet side. If you’re familiar with this, you’ve already talked about HTML submitting user input to the server through a form. So POST can submit data via forms. Let’s modify the webpage as follows:

<! -- visitFirstAutoServlet.html -->
<! DOCTYPEhtml>
<html>
  <head>
    <title>visitFirstAutoServlet.html</title>
  </head>
  <body>
    <form name="mainForm" method="post" action="/ServletDemo/FirstAutoServlet">
    	<input type="submit"></input>
    </form>
  </body>
</html>
Copy the code

Method =”post” (method=” get “, method=”post”, method=”post”) This is a class a servlet FirstAutoServlet, using the POST method. We successfully transferred to a post, this way it should be very careful, is action = “/ ServletDemo/FirstAutoServlet”. You need to know that the first/stands for 127.0.0.1:8080/, so write the project name first followed by the Servlet mapping name (/FirstAutoServlet).

OK, at this point, you only need to know:

1. Get 2 is used for direct access to the URL, and get 3 is used for direct access to the <a> tagCopy the code

It is enough. Finished.


Project source code, series of tutorials and more content, please visit panda programming network