javaWEB總結(9):自定義HttpServlet

前言:我們知道 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方法。

轉載于:https://www.cnblogs.com/tongrui/p/6193828.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/270447.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/270447.shtml
英文地址,請注明出處:http://en.pswp.cn/news/270447.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

硬件知識:u-boot和bootloader的區別

目錄 1、Bootloader 2、uboot 2.1.硬件管理 2.2.能夠完成鏡像燒錄&#xff08;刷機&#xff09; 2.3.uboot的“生命周期” 2.4.uboot要提供命令式shell界面 3、bootloader 與 uboot的區別 嵌入式軟件工程師聽說過 u-boot 和 bootloader&#xff0c;但很多工程師依然不知道他們到…

Java中局部變量必須初始化

Java中有兩種變量&#xff0c;局部變量和成員變量。 成員變量可以不進行初始化&#xff0c;虛擬機也會確保它有一個默認的值。 局部變量不能這樣做&#xff0c;我們必須對它進行賦值&#xff0c;才能使用它。

php 直播服務器搭建,基于Nginx搭建RTMP/HLS視頻直播服務器

1、Nginx環境搭建(基于lnmp環境)//下載并安裝lnmp環境wget -c http://soft.vpser.net/lnmp/lnmp1.3.tar.gz && tar zxf lnmp1.3.tar.gz && cd lnmp1.3 && ./install.sh lnmp安裝完成安裝完成后訪問服務器地址會出現如下界面lnmpPS&#xff1a;安裝時生…

辦公:office辦公軟件Excel表格的打印技巧

很多新手使用辦公軟件過程中&#xff0c;對于Excel的打印出現了諸多問題&#xff0c;今天我們就一起來看看表格打印的幾個技巧&#xff01; 打印預覽時網格線怎么不見了&#xff1f; 如何調整打印區域的頁邊距&#xff1f; 如何設置單色打印&#xff1f; ...... 這些打印時遇到…

vuejs 中如何優雅的獲取 Input 值

http://www.sunzhongwei.com/how-to-get-input-value-in-vuejs轉載于:https://www.cnblogs.com/benpaodexiaopangzi/p/6093275.html

線程池和線程詳細教程

1. 線程池的概念&#xff1a; 線程池就是首先創建一些線程&#xff0c;它們的集合稱為線程池。使用線程池可以很好地提高性能&#xff0c;線程池在系統啟動時即創建大量空閑的線程&#xff0c;程序將一個任務傳給線程池&#xff0c;線程池就會啟動一條線程來執行這個任務&#…

虛擬主機 php .htacess,LiteSpeed添加虛擬主機+支持htaccess圖文教程

上次給大家簡單介紹了Debian下手動安裝LiteSpeedMySQLPHP的教程(點擊查看)&#xff0c;但是這個教程還沒完&#xff0c;想要使用litespeed還要進入后臺進行設置&#xff0c;包括添加虛擬主機和.htaccess偽靜態的支持&#xff0c;本文就繼續這個話題給大家詳細做個圖文教程吧~為…

前端:uniapp封裝組件用法筆記

大家在做前端項目開發的時候&#xff0c;經常會遇到公用的一些頁面&#xff0c;比如搜索、列表、商品詳情卡片、評論列表等。為了提高開發效率、使代碼看起來更加簡潔&#xff0c;這個時候封裝相應的組件是最好的解決方案。今天小編給大家介紹一下如何在uniapp中封裝組件&#…

Angular的工作原理

首先上一小段代碼&#xff08;index.html&#xff09;&#xff0c;結合代碼我們來看看&#xff0c;angular一步一步都做了些什么。 <!doctype html> <html ng-app><head><script src"angular.js"></script></head><body>&…

php中等腰金字塔挖空,php 用for循環做,金字塔,菱形,空三角

echo "金字塔 style1";for($i1;$i<9;$i){for($k0;$kecho "*";}echo "";}echo "金字塔 style2";for($c5;$c>0;$c--){for($c10;$c1echo "*";}echo "";}echo "金字塔 style3";for($a0;$a<11;$a){…

網絡知識:四個網絡命令ping、arp、tracert、route的用法介紹

網絡相關的從業人員&#xff0c;都需要面對檢測和解決網絡故障的各種問題&#xff0c;實際案例中因為網絡導致的故障也是最多的&#xff0c;今天我們和大家一起來學習一下解決網絡故障時使用最多的四個網絡命令。希望對大家以后的實際工作中的故障排除起到作用。 1、Ping命令的…

jQuery擲骰子

網上找的jQuery擲骰子效果&#xff0c;測試兼容IE7及以上瀏覽器&#xff0c;IE6沒有測試 js代碼如下&#xff1a; 1 $(function(){2 var dice $("#dice");3 dice.click(function(){4 $(".wrap").append("<div iddice_mask><…

電腦知識:臺式電腦如何使用無線網上網

??作者主頁&#xff1a;IT技術分享社區 ??作者簡介&#xff1a;大家好,我是IT技術分享社區的博主&#xff0c;從事C#、Java開發九年&#xff0c;對數據庫、C#、Java、前端、運維、電腦技巧等經驗豐富。 ??個人榮譽&#xff1a; 數據庫領域優質創作者&#x1f3c6;&#x…

oracle的function的語法,Oracle function語法

2018-3-30 遇到需要使用SQL方法拼接字符串的情況&#xff0c;就研究了一下SQL簡單的方法應用--定義入參數[參數名 in 參數類型]create or replace function p_gettype(se_type in varchar2)--定義返回類型return varchar2isv_calling_type varchar2(45);v_called_type varchar2…

進程動態優先級調度

簡單的進程優先級動態調度 cup運行&#xff1a; 每執行一次&#xff0c;優先級減一&#xff0c;運行時間減一。 就緒隊列中的進程&#xff1a;每次按優先級降序排序&#xff08;優先級越大越優先執行&#xff09;&#xff0c;若優先級相等再按時間升序排序&#xff08;時間越小…

電腦維修:如何給筆記本電腦升級內存條

??作者主頁&#xff1a;IT技術分享社區 ??作者簡介&#xff1a;大家好,我是IT技術分享社區的博主&#xff0c;從事C#、Java開發九年&#xff0c;對數據庫、C#、Java、前端、運維、電腦技巧等經驗豐富。 ??個人榮譽&#xff1a; 數據庫領域優質創作者&#x1f3c6;&#x…

php動態添加查詢,php動態添加url查詢參數的方法,php動態url參數_PHP教程

php動態添加url查詢參數的方法&#xff0c;php動態url參數本文實例講述了php動態添加url查詢參數的方法。分享給大家供大家參考。具體分析如下&#xff1a;這段代碼可以動態為url添加key-value查詢參數&#xff0c;如果參數已經存在則會用新的進行覆蓋function add_querystring…

Object o = new Object()在內存中占幾個字節

CAS&#xff1a; Compare and Swap&#xff0c;即比較再交換。 jdk5增加了并發包java.util.concurrent.*,其下面的類使用CAS算法實現了區別于synchronouse同步鎖的一種樂觀鎖。JDK 5之前Java語言是靠synchronized關鍵字保證同步的&#xff0c;這是一種獨占鎖&#xff0c;也是…

MYSQL筆記:刪除操作Delete、Truncate、Drop用法比較

今天小編給大家梳理一下MYSQL刪除操作Delete、Truncate、Drop用法有什么區別&#xff0c;到底該如何合理使用&#xff0c;希望對大家能有幫助&#xff01;1、執行速度比較Delete、Truncate、Drop關鍵字都可以刪除數據drop>truncate>delete2、原理方面2.1 deletedelete屬于…