HttpServlet详解
-----------------------HttpServlet详解-------------------专注HTTP请求的Servlet1 写一个专门处理HTTP请求的Servlet因为现在我们的请求都是基于HTTP协议的,所以我们应该专门为HTTP请求写一个Servlet做为通用父类。对于专注于HTTP的Servlet,我们需要处理以下几个问题:service()方法的...
-----------------------HttpServlet详解-------------------
专注HTTP请求的Servlet
1 写一个专门处理HTTP请求的Servlet
因为现在我们的请求都是基于HTTP协议的,所以我们应该专门为HTTP请求写一个Servlet做为通用父类。
对于专注于HTTP的Servlet,我们需要处理以下几个问题:
service()方法的参数ServletRequest和ServletResponse,但因为所有的请求都是HTTP请求,所以传递给service()就去的参数其实是HttpServletRequest和HttpServletResponse对象。如果子类希望使用的是HttpServletRequest,而不是ServletRequest,那么它需要自己去做强转。
Servlet:SUN公司 设计之初 以后不仅仅依赖HTTP协议
GenericServlet --- 通用Servlet
service(ServletRequest req, ServletResponse res)
HttpServlet --- 与协议相关的Servlet
service(HttpServletRequest req, HttpServletResponse resp)
以后再写Servlet 继承 HttpServlet
Servlet --- 一个标准
|
GenericServlet --- 是Servlet接口子类
|
HttpServlet --- 是GenericServlet子类,一个专门处理Http请求的Servlet
HttpServlet
两个Service方法
* 父类service 调用子类service 使用子类service方法就可以
* 子类中service 根据请求方式不同 调用不同的方法
只需要重写doGet和doPost就行.
写一个Servlet 继承HttpServlet
重写doGet和doPost 方法.
没错,Java中已经存在了javax.servlet.http.HttpServlet类。打开源代码看看这个类!
2 HTTP请求方法
HTTP请求方法不只是GET和POST,还有其他的方法,但基本上用不上。这里只是简单介绍一下。你自己心里有个数,HTTP请求除了GET和POST之外还有别的就行了。
- GET通过请求URI得到资源
- POST用于添加新的内容
- PUT用于修改某个内容
- DELETE,删除某个内容
- CONNECT用于代理进行传输,如使用SSL
- OPTIONS询问可以执行哪些方法
- PATCH部分文档更改
- RACE用于远程诊断服务器
- HEAD类似于GET, 但是不返回body信息,用于检查对象是否存在,以及得到对象的元数据
- TRACE用于远程诊断服务器
代码示例:
|
package com.rl.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * HttpServlet详解 * * @author 69301 doGet是给get方式的http的请求做相应的 doPost是给post方式的http的请求做相应的 * */ public class HttpServletDemo extends HttpServlet {
@Override public void init() throws ServletException { System.out.println("实例被创建了"); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet方法被调用了"); resp.getOutputStream().write("doGet方法被调用".getBytes()); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost方法被调用了"); doGet(req, resp); }
} |
Index.jsp代码:
|
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head>
<body> <form action="httpRequest" method="post"> <input type="submit" value="submit"/> </form> </body> </html> |
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">
<servlet> <servlet-name>httpServlet</servlet-name> <servlet-class>com.rl.servlet.HttpServletDemo</servlet-class> </servlet>
<servlet-mapping> <servlet-name>httpServlet</servlet-name> <url-pattern>/httpRequest</url-pattern> </servlet-mapping> </web-app> |
更多推荐



所有评论(0)