1. Basic Servlet structure in Servlet tutorial
Next, let's talk about the simplest servlet framework for processing user GET requests. A GET request is a request generated when you enter an address in the address bar of a browser, click Connect on a webpage, or generate an HTML form without defining a method. Servlets can also easily process form submission (POST). We will talk about Processing Form submission in the following sections.
- Import java. io .*;
- Import javax. servlet .*;
- Import javax. servlet. http .*;
- Public class SomeServlet extends HttpServlet {
- Public void doGet (
- HttpServletRequest request,
- HttpServletResponse response)
- Throws ServletException, IOException {
-
- // Use "request" to read http information headers such as cookies)
- // And HTML form data (such as user input and submitted data)
- // Use "response" to specify the http response and http Header
- // (For example, specify the information type and set the cookie ).
-
- PrintWriterOut=Response. GetWriter ();
- // Output content to the browser with "out"
- }}
Whether using the doGet or doPost method, servlet must use the HttpServlet extension class. These methods can be divided into two types: HttpServletRequest and HttpServletResponse. HttpServletRequest contains methods to obtain form data and HTTP information headers. HttpServletResponse indicates the HTTP Response (200,404, etc .) , Information header (Content-Type, Set-Cookie, etc .) More importantly, you can use the PrintWriter method to output information to the client. Note that the doGet and doPost Methods throw two exceptions, so they must be included in the definition. To use PrintWriter, HttpServlet, and HttpServletRequest, The HttpServletResponse method must also introduce java. io, javax. servlet, and javax. servlet. http respectively. In general, doGet and doPost are called by the service method, but sometimes you may want to directly use your own service method over the service method, for example, define a servlet that can process both GET and POST requests.
2. A simple Servlet in the Servlet tutorial-generate saved text
◆ The following is an example of a simple servlet that generates text.
- HelloWorld.java
-
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class HelloWorld extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- PrintWriter out = response.getWriter();
- out.println("Hello World");
- }
- }
◆ Compile and install Servlet
Note that the servlet installation method varies with the web server. For more information, see the installation documentation for your web server. Java Web Server (JWS) 2.0 comes with an online instance. In JWS, the servlet program must be placed in the servlets directory of the installation directory to run. If the web server is automatically used by multiple users and does not have a good underlying structure of the virtual server to avoid conflicts, you can also create an independent package named hall in the servlets directory like me) create a hall sub-directory under the servlets directory and put the servlet you wrote in this directory. Here I put HelloWorld. java under the hall directory. The installation of most other servers is similar. The servlet and JSP examples in this tutorial are all tested and passed in BEA Weblogic and IBM WebSphere 3.0. WebSphere has an excellent mechanism for virtual servers, so it is not necessary to use packages separately for name conflicts.
If you have never used a package before, you can use two methods to compile class files in the package.
One way is to set a directory in CLASSPATH that points to the directory that contains your servlet File, so that you can compile normally under this directory. For example, if the servlet directory in Windows is C: JavaWebServerservlets and the package name (subdirectory) is hall, set it in the dos window as follows:
- DOS> set CCLASSPATH=C:JavaWebServerservlets;%CLASSPATH%
- DOS> cd C:JavaWebServerservletshall
- DOS> javac YourServlet.java
Step 1: set the path. We recommend that you do not open a dos window every time. In Windows 95/98, add the "set CLASSPATH =..." expression to the autoexec. bat file, and point the CLASSPATH to servlet. jar and jsp. jar. Under NT, choose "Start Menu"> "Settings"> "Control Panel", select "system", and enter the variable name and variable value. Note that if your package name is in name1.name2. name3 format, you should also set CLASSPATH to point to the top layer of the package, that is, name1 ).
The second method is to go to the upper directory of the package, and then execute "javac directoryYourServlet. java "(in Windows) or unix" javac directory/YourServlet. java "(note that a positive oblique rod is used in Unix ). For example, if the servlet directory in windows is C: JavaWebServerservlets and the package name is hall, perform the following operations:
- DOS> cd C:JavaWebServerservlets
- DOS> javac hallYourServlet.java
Note that in Windows, most JDK 1.1 versions of javac require a backslice lever. Later, it was corrected in JDK 1.2. However, many web servers are configured based on JDK, for the sake of convenience, many servlet authors still stick to the anti-oblique rod.
The-d option of javac can be used to place the. class file and the source code file in different places.
◆ Servlet tutorial run Servlet
Generally, servlets are stored in the servlets directory under the JWS installation directory and called through http: // host/Servlet/ServletName. Note that the Servlets in the Servlets directory contains "s", but not in the URL address bar.
The preceding example is put in the hall package, so the following code is called: http: // host/servlet/hall. HelloWorld. Other servers may be slightly different in Servlet directory placement and calling. Most servers allow you to define the servlet directory ing, so a servlet can also be called through http: // host/any-path/any-file.html. For details, refer to the server documentation.
The basics of the Servlet tutorial are fragmented, but this Servlet tutorial will help you gradually understand and improve.
- Automatic jump to error page for JSP Servlet instance
- 8-point discussion on optimizing JSP Servlet applications
- Servlet import event-driven technology in JSP development
- What is JSP and comparison with Servlet?
- Configuration of JSP, Servlet, and Bean in Tomcat