Servlet configuration and Servlet Configuration
Today, I came into contact with Servlet. Servlet is a Java program. It is a program Servlet that runs on the server to process client requests and respond to them. It is the initial version in the early stage. this section describes the Servlet configuration.
Case study: web project under WebRoot/WEB-INF/web. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Register servlet --> <servlet-name> HelloServlet </servlet-name> <servlet-class> servlet. HelloServlet </servlet-class> <! -- Initialization parameters: managed by the current servlet --> <init-param> <param-name> name </param-name> <param-value> James </param-value> </init-param> </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> query </servlet-name> <servlet-class> servlet. query </servlet-class> </servlet> <servlet-mapping> <servlet-na Me> query </servlet-name> <url-pattern>/servlet/query </url-pattern> </servlet-mapping> <! -- Map servlet --> <servlet-mapping> <servlet-name> HelloServlet </servlet-name> <url-pattern>/HelloServlet </url-pattern> </servlet-mapping> <! -- Context parameters: managed by web containers --> <context-param> <param-name> name </param-name> <param-value> admin </param-value> </context-param> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>
JAVA code:
Package servlet; import java. io. IOException; import java. io. printWriter; import javax. faces. application. application; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import com. sun. corba. se. impl. interceptors. PINoOpHandlerImpl; public class HelloServl Et extends HttpServlet {/*** servlet lifecycle ** 1 \ instantiate servlet (implemented by web containers) * use function * 1. Perform initialization only once; execute (execute the init () method to reflect) * 2. Actually process client requests (execute the service () method) * OUTPUT response information * 3. Close the web container; execute only once; execute the destroy servlet (execute the destory () method) * // * (non-Javadoc) * rewrite the parent class initialization method * @ see javax. servlet. genericServlet # init () */@ Override public void init () throws ServletException {// TODO Auto-generated method stub System. out. Println ("servlet's first execution method !!! "); // Get the servlet initialization System. out. println ("initialization parameter:" + this. getServletConfig (). getInitParameter ("name"); // obtain the context parameter System. out. println ("context parameter:" + this. getServletContext (). getInitParameter ("name");}/* (non-Javadoc) * call this method for requests submitted in GET submission mode * This method is executed when the submission mode is not specified by default * @ see javax. servlet. http. httpServlet # doGet (javax. servlet. http. httpServletRequest, javax. servlet. http. httpServletResponse) * // @ Override protected void do Get (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub}/* (non-Javadoc) * call this method for POST-submitted requests * @ see javax. servlet. http. httpServlet # doPost (javax. servlet. http. httpServletRequest, javax. servlet. http. httpServletResponse) * // @ Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub/*** process client requests */System. out. println ("executing service method processing requests"); // receiving parameter String username = request. getParameter ("username"); String password = request. getParameter ("password"); if (username. equals ("admin") & password. equals ("123456") {// equivalent to the session HttpSession session = request on the jsp page. getSession (); Application app = (Application) getServlet Context (); request. setAttribute ("username", username); // The request is displayed on the success page. getRequestDispatcher ("/success. jsp "). forward (request, response);}/*** response to client request * // tell the client how to explain the returned content response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); out. print ("<HTML>"); out. print ("
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 query extends HttpServlet { /** * Constructor of the object. */ public query() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}
JSP code:
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">