Servlet通用写法

本文介绍了Servlet的通用写法,重点讲解了如何创建一个继承自HttpServlet的类,并复写doGet和doPost方法。同时,提到了在web.xml中的配置方式,以实现对HTTP请求的处理。通过这种方式,可以更高效地管理Servlet的生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

###Servlet的通用写法

       Servlet (接口)
            |
            |
        GenericServlet
            |
            |
        HttpServlet (用于处理http的请求)

        

定义一个类,继承HttpServlet 复写doGet 和 doPost

HttpServlet02:

package com.yuming.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet02 extends HttpServlet{
	
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
		System.out.println("HelloServlet02  service方法");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
		System.out.println("Get 方法............");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
		System.out.println("POST 方法------------------");
	}

}

不过,以后不用service方法:

HttpServlet02:

package com.yuming.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet02 extends HttpServlet{
	
	/*@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
		System.out.println("HelloServlet02  service方法");
	}*/
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
		System.out.println("Get 方法............");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
		System.out.println("POST 方法------------------");
	}

}

 

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>helloweb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
   <!-- 向tomcat报告, 我这个应用里面有这个servlet, 名字叫做HelloServlet , 具体的路径是com.yuming.servlet.HelloServlet -->
  <servlet>
  <servlet-name>HelloServlet</servlet-name>
  <servlet-class>com.yuming.servlet.HelloServlet</servlet-class>
  </servlet>
  
  <!-- 注册servlet的映射。  servletName : 找到上面注册的具体servlet,  url-pattern: 在地址栏上的path 一定要以/打头 -->
  <servlet-mapping>
  <servlet-name>HelloServlet</servlet-name>
  <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
  <servlet-name>HelloServlet02</servlet-name>
  <servlet-class>com.yuming.servlet.HelloServlet02</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>HelloServlet02</servlet-name>
  <url-pattern>/HelloServlet02</url-pattern>
  </servlet-mapping>
  
</web-app>

运行结果:


 

Exception report nessage An exception occurred processing Isp page /resource/jsp/jsperrorjsp at line 29 escription The server encountered an interal error that prevented it from fulfilling this request. xception rg. apache. jasper. JasperException: An exception occurred processing TSP page /resource/jsp/jsperror.jsp at line 29 C [-- 父 5 request.setAttribute("KMSS Parameter_ContextPath", Ul ContextPath+"/); request,setAttribute("KlSS Parameter ResPath", lUl ContextPath+"/resource/"). request.setAttribute("KlsS Parameter StylePath", LUl ContextPath+"/resource/style/default/") request. setAttribute("KlsS _Parameter_CurrentUserId", Userltil.getKlsSUser (request).getUserId()): request. setattribute("Klss Parameter Lang", Userltil. getklsUser (request).getLocale(),tostring().toLoverCase().replace(’’, ’_')); 装》 acktrace: org. apache. jasper. servlet.JspServletWrapper.handleJspException(JspServletWrapper. java: 579) org.apache.jasper.servlet.JspServletWrapper,service(TspServletWrapper. java: 481) org.apache.iasper.gervlet. TspSerwlet,serviceTspFile(TspServlet iava:385) org.apache iasper.servlet TspServlet.service(TspServlet.iava: 329) javax.servlet.http.HttpServlet,service(HttpServlet.iava: 729) org.apache.tomcat.websocket,server.WsFilter.doFilter (sFilter. iava: 52) org.apache. jasper.runtime.PageContextImpl,doForward(PageContextImpl.iava: 742) org,apache,jasper,runtime,PageContextImpl,forward(PageContextImpl. iava: 712) org, apache. iasper, runtime.PageContextImpl. doHandlePageException(PageContextImpl. iava. 866) org.apache.iasper.runtime.PageContextImpl,handlePageException(PageContextImpl. java: 838) org.apache.jsp.sys.portal.page_jsp._jspService(page_jsp.java:167) org.apache.jasper.runtime.HttpIspBase,service(HttpTspBase. iava: 70) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org,apache.jasper.servlet,TspServletWrapper.service(TspServletWrapper. iava: 443) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java: 385)
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值