Let's talk about the simplest servlet framework to handle user get requests. A GET request is a request that a user enters an address in the browser's address bar, clicks a connection on a Web page, or generates an HTML form that does not have a defined method. Servlets also handles form submission (POST) easily, and we'll talk about processing form submissions in the later 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 {
Read HTTP headers (such as cookies) with "request"
and HTML form data (such as user input and submitted data)
Specifying HTTP responses and HTTP headers with "response"
(such as specifying the type of information, setting cookies).
PrintWriter out = Response.getwriter ();
Output with "out" to the browser
} }
The servlet uses the HttpServlet extension class for both Doget and Dopost methods. These methods can be divided into two categories: HttpServletRequest and HttpServletResponse. HttpServletRequest contains methods for obtaining first-order information about form data and HTTP information. HttpServletResponse contains the indicated HTTP response (200, 404, and so on). ), Information head (Content-type, Set-cookie, etc.). method, and 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 be introduced into java.io, Javax.servlet and Javax.servlet.http. Generally speaking, doget and Dopost are called by the service method, but sometimes you may want to bypass the service method and use your own defined service method, such as defining a servlet that can handle both get and post requests.
I translate from http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
There are irregularities, please advise!
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.