前言:我们知道 MyHttpServlet是MyGenericServlet的子类,MyHttpServlet会继承父类的方法,可是却很少去追问MyHttpServlet中的doGet方法和doPost方法是如何进行判断的,本文主要做一个小例子,进一步理解HttpServlet。有些代码前文多次用到,这里不再叙述。
1.项目结构
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>javaWeb_09</display-name><welcome-file-list><welcome-file>login.jsp</welcome-file></welcome-file-list><servlet><servlet-name>myHttpServletExtends</servlet-name><servlet-class>com.dao.chu.MyHttpServletExtends</servlet-class></servlet><servlet-mapping><servlet-name>myHttpServletExtends</servlet-name><url-pattern>/myHttpServletExtends</url-pattern></servlet-mapping></web-app>
3.login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登陆页</title>
</head>
<body><form action="myHttpServletExtends" method="post">用户名:<input type="text" name="user"><br><br>密码 <input type="password" name="password"><br><br><!-- 一组信息 -->interesting:<input type="checkbox" name="interesting" value="reading">Reading<input type="checkbox" name="interesting" value="writing">Writing<input type="checkbox" name="interesting" value="football">Football<input type="checkbox" name="interesting" value="game">Game<input type="checkbox" name="interesting" value="shopping">Shopping<input type="checkbox" name="interesting" value="party">Party<input type="checkbox" name="interesting" value="TV">TV<br><br><input type="submit" value="提交"></form></body>
</html>
4.MyGenericServlet
package com.dao.chu;import java.util.Enumeration;import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;/*** * <p>* Title: MyGenericServlet* </p>* <p>* Description: 自定义MyGenericServlet* </p>*/
public abstract class MyGenericServlet implements Servlet, ServletConfig {// ServletConfig对象,在init方法执行后赋值private ServletConfig servletConfig;/*** 实现Servlet的方法*/@Overridepublic void destroy() {}@Overridepublic String getServletInfo() {return "";}// ServletConfig对象的get方法@Overridepublic ServletConfig getServletConfig() {return servletConfig;}@Overridepublic void init(ServletConfig servletConfig) throws ServletException {this.servletConfig = servletConfig;init();}// 空的init函数,用来给子类继承,防止覆盖init(ServletConfig servletConfig)方法引起的错误public void init() {}//抽象方法,必需被子类实现@Overridepublic abstract void service(ServletRequest servletRequest,ServletResponse servletResponse);/*** 实现ServletConfig的方法*/@Overridepublic String getServletName() {return getServletConfig().getServletName();}@Overridepublic ServletContext getServletContext() {return getServletConfig().getServletContext();}@Overridepublic String getInitParameter(String paramString) {return getServletConfig().getInitParameter(paramString);}@Overridepublic Enumeration<String> getInitParameterNames() {return getServletConfig().getInitParameterNames();}}
5.MyHttpServlet
package com.dao.chu;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** * <p>* Title: MyHttpServlet* </p>* <p>* Description:自定义 HttpServlet* </p>*/
public abstract class MyHttpServlet extends MyGenericServlet {private static final String METHED_GET="GET";private static final String METHED_POST="POST";@Overridepublic void service(ServletRequest servletRequest,ServletResponse servletResponse) {try {// 强转类型HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;// 调用service(HttpServletRequest,HttpServletResponse)方法service(request, response);} catch (Exception e) {e.printStackTrace();}}protected void service(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {// 根据请求方式,决定使用的方法String method = httpServletRequest.getMethod();if (method.equalsIgnoreCase(METHED_GET)) {doGet(httpServletRequest, httpServletResponse);} else if (method.equalsIgnoreCase(METHED_POST)) {doPost(httpServletRequest, httpServletResponse);}}protected void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {}protected void doGet(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {}}
6.MyHttpServletExtends
package com.dao.chu;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** * <p>* Title: MyHttpServletExtends* </p>* <p>* Description: 继承MyHttpServletExtends的类* </p>*/
public class MyHttpServletExtends extends MyHttpServlet {/*** Post请求方式*/@Overridepublic void doPost(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {try {PrintWriter out = httpServletResponse.getWriter();out.print("hello Post");} catch (IOException e) {e.printStackTrace();}}/*** Get请求方式*/@Overridepublic void doGet(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {try {PrintWriter out = httpServletResponse.getWriter();out.print("hello Get");} catch (IOException e) {e.printStackTrace();}}}
7.页面请求提交后,可以根据判断的请求方式到相应的方法中执行:
所以HttpServlet中重载了service方法,主要作用是根据请求类型调用相应的doXXX方法。