Basic Structure and simple operation of Servlet tutorial

Source: Internet
Author: User

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.

 
 
  1. Import java. io .*;
  2. Import javax. servlet .*;
  3. Import javax. servlet. http .*;
  4. Public class SomeServlet extends HttpServlet {
  5. Public void doGet (
  6. HttpServletRequest request,
  7. HttpServletResponse response)
  8. Throws ServletException, IOException {
  9.  
  10. // Use "request" to read http information headers such as cookies)
  11. // And HTML form data (such as user input and submitted data)
  12. // Use "response" to specify the http response and http Header
  13. // (For example, specify the information type and set the cookie ).
  14.  
  15. PrintWriterOut=Response. GetWriter ();
  16. // Output content to the browser with "out"
  17. }}

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.

 
 
  1. HelloWorld.java   
  2.  
  3. import java.io.*;   
  4. import javax.servlet.*;   
  5. import javax.servlet.http.*;   
  6. public class HelloWorld extends HttpServlet {   
  7. public void doGet(HttpServletRequest request, HttpServletResponse response)   
  8. throws ServletException, IOException {  
  9. PrintWriter out = response.getWriter();   
  10. out.println("Hello World");   
  11. }   
  12. }  

◆ 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:

 
 
  1. DOS> set CCLASSPATH=C:JavaWebServerservlets;%CLASSPATH%   
  2. DOS> cd C:JavaWebServerservletshall   
  3. 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:

 
 
  1. DOS> cd C:JavaWebServerservlets   
  2. 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.

  1. Automatic jump to error page for JSP Servlet instance
  2. 8-point discussion on optimizing JSP Servlet applications
  3. Servlet import event-driven technology in JSP development
  4. What is JSP and comparison with Servlet?
  5. Configuration of JSP, Servlet, and Bean in Tomcat

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.