A simple Servlet example

Source: Internet
Author: User

When a user sends a request to the client, such as [plain] view plaincopyhttp: // localhost: 8088/ServApp/myreq. cg? Param1 = param when a Socket request arrives at the server (assuming it is a Tomcat server), after a series of encapsulation processes on the Tomcat server, the request is encapsulated into an HttpServletRequest object and handed over to the corresponding Servlet. The Simple Sequence diagram is as follows: Here, we only focus on step 1. In step 2, the request object is passed to the Container component for execution. When the Servlet object obtains the request (httpservletrequest), it calls the relevant function (Service) for processing. Below is a simple example to illustrate the current step. 1. Compile the request processing class (EntryServ) [java] package com. serv; import java. io. IOException; import java. io. printWriter; import javax. servlet. requestDispatcher; import javax. servlet. servletContext; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class Entry Serv extends HttpServlet {/*****/private static final long serialVersionUID = 1L; @ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stub ServletContext serv = getServletContext (); // obtain request-related information String Contextpath = req. getContextPath (); String CharacterEncoding = req. getCharacterEncoding (); Stri Ng ContentType = req. getContentType (); String LocalAddr = req. getLocalAddr (); String LocalName = req. getLocalName (); String Method = req. getMethod (); String Parameter = req. getParameter ("param1"); String Protocol = req. getProtocol (); String ServerName = req. getServerName (); Cookie [] Cookies = req. getCookies (); String ServletPath = req. getServletPath (); String RemoteAddr = req. getRemoteAddr (); Stri Ng RemoteHost = req. getRemoteHost (); String RequestURI = req. getRequestURI (); String RequestedSessionId = req. getRequestedSessionId (); req. setCharacterEncoding ("gbk"); req. setAttribute ("oneattr", "setted"); // response responds to the request and outputs some resp content of the request. setContentType ("text/html; charset = gbk"); // set the response page character encoding PrintWriter out = resp. getWriter (); out. println ("<font color = red>

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.