1.提交表单的常用方法:
get:将请求参数显示在url中,调用Servlet的doGet方法
post:不在url中显示请求参数,调用Servlet的doPost方法
2.Servlet生命周期
加载实例化Servlet:创建一个Servlet实例
调用init方法:仅被调用一次
调用service方法(doGet或doPost):被调用多次(每次请求调用)
调用destroy方法:调用一次(销毁Servlet)
3.请求转发和请求重定向
1 package com.amaker.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.sun.org.apache.xpath.internal.Arg; 11 12 public class BasicServlet extends HttpServlet{ 13 14 public BasicServlet() { 15 System.out.println("BasicServlet"); 16 } 17 @Override 18 public void init() throws ServletException { 19 20 String url = this.getServletContext().getInitParameter("url"); 21 System.out.println(url); 22 23 System.out.println("init"); 24 String arg = this.getInitParameter("username"); 25 System.out.println(arg); 26 } 27 @Override 28 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 29 30 String username = request.getParameter("username"); //拿到请求参数username 31 if(username!=null&&username.equals("amaker")){ 32 // request.getRequestDispatcher("/success.html").forward(request, response); // 请求转发 33 response.sendRedirect("https://www.baidu.com/"); // 请求重定向,不共享request里面的数据内容 34 } 35 else{ 36 // request.getRequestDispatcher("/fail.html").forward(request, response); 37 response.sendRedirect("https://translate.google.cn/"); 38 } 39 40 System.out.println("doGet"); 41 } 42 43 @Override 44 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 System.out.println("doPost"); 46 } 47 /* 48 @Override 49 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 50 System.out.println("service"); 51 }*/ 52 53 @Override 54 public void destroy() { 55 System.out.println("destroy"); 56 } 57 }
web.xml为:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>2_Servlet_Basic</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <context-param> 14 <param-name>url</param-name> 15 <param-value>url...</param-value> 16 </context-param> 17 18 19 <servlet> 20 <description></description> 21 <display-name>BasicServlet</display-name> 22 <servlet-name>BasicServlet</servlet-name> 23 <servlet-class>com.amaker.servlet.BasicServlet</servlet-class> 24 25 <init-param> 26 <param-name>username</param-name> 27 <param-value>amaker</param-value> 28 </init-param> 29 </servlet> 30 <servlet-mapping> 31 <servlet-name>BasicServlet</servlet-name> 32 <url-pattern>/BasicServlet</url-pattern> 33 </servlet-mapping> 34 </web-app>
Test.html为:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form name="f1" action="/2_Servlet_Basic/BasicServlet" method="get"> 9 <table> 10 <tr> 11 <td>username:</td> 12 <td><input type="text" name="username"></td> 13 </tr> 14 <tr> 15 <td>password:</td> 16 <td><input type="text" name="password"></td> 17 </tr> 18 <tr> 19 <td colspan="2" align="center"><input type="submit"></td> 20 </tr> 21 </table> 22 </form> 23 </body> 24 </html>
运行后为:
提交查询内容后的输出为: