javaWEB总结(9):自定义HttpServlet

2017-01-13 15:02:07来源:csdn作者:daochuwenziyao人点击


前言:我们知道 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.MyGenericServletpackage 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的方法
*/@Override
public void destroy() {}@Override
public String getServletInfo() {
return "";
}// ServletConfig对象的get方法
@Override
public ServletConfig getServletConfig() {
return servletConfig;
}@Override
public void init(ServletConfig servletConfig) throws ServletException {this.servletConfig = servletConfig;
init();}// 空的init函数,用来给子类继承,防止覆盖init(ServletConfig servletConfig)方法引起的错误
public void init() {}//抽象方法,必需被子类实现
@Override
public abstract void service(ServletRequest servletRequest,
ServletResponse servletResponse);/**
* 实现ServletConfig的方法
*/@Override
public String getServletName() {
return getServletConfig().getServletName();
}@Override
public ServletContext getServletContext() {
return getServletConfig().getServletContext();
}@Override
public String getInitParameter(String paramString) {
return getServletConfig().getInitParameter(paramString);
}@Override
public Enumeration<String> getInitParameterNames() {
return getServletConfig().getInitParameterNames();
}}5.MyHttpServletpackage 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";@Override
public 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.MyHttpServletExtendspackage 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请求方式
*/
@Override
public void doPost(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {try {
PrintWriter out = httpServletResponse.getWriter();
out.print("hello Post");
} catch (IOException e) {
e.printStackTrace();
}
}/**
* Get请求方式
*/
@Override
public void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {try {
PrintWriter out = httpServletResponse.getWriter();
out.print("hello Get");
} catch (IOException e) {
e.printStackTrace();
}}}7.页面请求提交后,可以根据判断的请求方式到相应的方法中执行:






所以HttpServlet中重载了service方法,主要作用是根据请求类型调用相应的doXXX方法。

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台