Servlet从入门到精通三
2012-11-29 08:46
363 查看
一、HttpServlet和一些开发细节
Servlet接口与两个默认实现类:GenericServlet、HttpServlet
1、HttpServlet指能够处理HTTP请求的Servlet,它在原有Servlet接口上添加了一些与HTTP协议处理方法,它比Servlet接口的功能更为强大。因此开发人员在编写Servlet时,通常应该继承这个类,而避免直接去实现Servlet接口。
2、HttpServlet在实现Servlet接口时,覆写了service()方法,改方法体内的代码会自动判断用户的请求方式,如为GET请求,则调用HttpServlet的doGet方法,如为Post请求,则调用doPost方法。因此,开发人员在编写Servlet时,通常只需要覆写doGet或doPost方法,而不要去覆写service方法。
查看servlet源码,看HttpServlet源码:
3、实例
1)建Web project
2)新建一个servlet,如下
3)点next,再点finish
4)写代码:
5)、发布,打开Tomcat,浏览器中输入:
http://localhost:8088/JavaWebChuan/servlet/ServletDemo1,
可以看见 Hello,HttpServlet!
4、Myeclipse中改Servlet模板:
进到MyEclipse安装目录 D:\MyEclipse 7.0M1,搜索servlet.java
找到Servlet.java这个文件,把内容改为:
#---------------------------------------------#
# <aw:description>Template for Servlet</aw:description>
# <aw:version>1.1</aw:version>
# <aw:date>04/05/2003</aw:date>
# <aw:author>Ferret Renaud</aw:author>
#---------------------------------------------#
<aw:import>java.io.IOException</aw:import>
<aw:import>java.io.PrintWriter</aw:import>
<aw:import>javax.servlet.ServletException</aw:import>
<aw:import>javax.servlet.http.HttpServlet</aw:import>
<aw:import>javax.servlet.http.HttpServletRequest</aw:import>
<aw:import>javax.servlet.http.HttpServletResponse</aw:import>
<aw:parentClass>javax.servlet.http.HttpServlet</aw:parentClass>
<aw:constructor name="c1">
/**
* Constructor of the object.
*/
public <aw:className/>() {
super();
}
</aw:constructor>
<aw:method name="doGet">
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
</aw:method>
<aw:method name="doPost">
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
</aw:method>
<aw:method name="doPut">
/**
* The doPut method of the servlet. <br>
*
* This method is called when a HTTP put request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
</aw:method>
<aw:method name="doDelete">
/**
* The doDelete method of the servlet. <br>
*
* This method is called when a HTTP delete request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
</aw:method>
<aw:method name="init">
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
</aw:method>
<aw:method name="destroy">
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
</aw:method>
<aw:method name="getServletInfo">
/**
* Returns information about the servlet, such as
* author, version, and copyright.
*
* @return String information about this servlet
*/
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
</aw:method>
保存,即可
Servlet接口与两个默认实现类:GenericServlet、HttpServlet
1、HttpServlet指能够处理HTTP请求的Servlet,它在原有Servlet接口上添加了一些与HTTP协议处理方法,它比Servlet接口的功能更为强大。因此开发人员在编写Servlet时,通常应该继承这个类,而避免直接去实现Servlet接口。
2、HttpServlet在实现Servlet接口时,覆写了service()方法,改方法体内的代码会自动判断用户的请求方式,如为GET请求,则调用HttpServlet的doGet方法,如为Post请求,则调用doPost方法。因此,开发人员在编写Servlet时,通常只需要覆写doGet或doPost方法,而不要去覆写service方法。
查看servlet源码,看HttpServlet源码:
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if (method.equals(METHOD_GET)) { long lastModified = getLastModified(req); if (lastModified == -1) { doGet(req, resp); } else { long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); if (ifModifiedSince < (lastModified / 1000 * 1000)) { maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } } } else if (method.equals(METHOD_HEAD)) { long lastModified = getLastModified(req); maybeSetLastModified(resp, lastModified); doHead(req, resp); } else if (method.equals(METHOD_POST)) { doPost(req, resp); } else if (method.equals(METHOD_PUT)) { doPut(req, resp); } else if (method.equals(METHOD_DELETE)) { doDelete(req, resp); } else if (method.equals(METHOD_OPTIONS)) { doOptions(req,resp); } else if (method.equals(METHOD_TRACE)) { doTrace(req,resp); } else { String errMsg = lStrings.getString("http.method_not_implemented"); Object[] errArgs = new Object[1]; errArgs[0] = method; errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); } }
3、实例
1)建Web project
2)新建一个servlet,如下
3)点next,再点finish
4)写代码:
package cn.itcast; 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 ServletDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getOutputStream().write("Hello,HttpServlet!".getBytes()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); //这句话的意思:客户端不管是Get方式还是Post方式,都由doGet来响应。 } }
5)、发布,打开Tomcat,浏览器中输入:
http://localhost:8088/JavaWebChuan/servlet/ServletDemo1,
可以看见 Hello,HttpServlet!
4、Myeclipse中改Servlet模板:
进到MyEclipse安装目录 D:\MyEclipse 7.0M1,搜索servlet.java
找到Servlet.java这个文件,把内容改为:
#---------------------------------------------#
# <aw:description>Template for Servlet</aw:description>
# <aw:version>1.1</aw:version>
# <aw:date>04/05/2003</aw:date>
# <aw:author>Ferret Renaud</aw:author>
#---------------------------------------------#
<aw:import>java.io.IOException</aw:import>
<aw:import>java.io.PrintWriter</aw:import>
<aw:import>javax.servlet.ServletException</aw:import>
<aw:import>javax.servlet.http.HttpServlet</aw:import>
<aw:import>javax.servlet.http.HttpServletRequest</aw:import>
<aw:import>javax.servlet.http.HttpServletResponse</aw:import>
<aw:parentClass>javax.servlet.http.HttpServlet</aw:parentClass>
<aw:constructor name="c1">
/**
* Constructor of the object.
*/
public <aw:className/>() {
super();
}
</aw:constructor>
<aw:method name="doGet">
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
</aw:method>
<aw:method name="doPost">
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
</aw:method>
<aw:method name="doPut">
/**
* The doPut method of the servlet. <br>
*
* This method is called when a HTTP put request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
</aw:method>
<aw:method name="doDelete">
/**
* The doDelete method of the servlet. <br>
*
* This method is called when a HTTP delete request is received.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Put your code here
}
</aw:method>
<aw:method name="init">
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
</aw:method>
<aw:method name="destroy">
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
</aw:method>
<aw:method name="getServletInfo">
/**
* Returns information about the servlet, such as
* author, version, and copyright.
*
* @return String information about this servlet
*/
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
</aw:method>
保存,即可
相关文章推荐
- Servlet从入门到精通七——ServletContext
- Servlet从入门打精通一——入门实例helloworld!
- Servlet从入门到精通四——Servlet开发中的细节
- Servlet从入门到精通九——Request
- Servlet从入门到精通十 ——会话管理——Cookie
- JAVA WEB从入门到精通day10 Servlet详解
- Servlet从入门到精通二
- Servlet从入门到精通
- Java网络编程从入门到精通 (9):使用isXxx方法判断地址类型
- MySoft.Data从入门到精通系列(二)【生成实体续(插件的使用)】
- MDT 2013 从入门到精通之LTI rules
- Redis入门到精通-Redis集群搭建
- 腾讯云使用教程 基本工具 开发工具软件 从入门到精通 图文教程
- 【Github教程】史上最全github用法:github入门到精通
- java web入门--004【servlet的service方法】
- Python 3从入门到精通2-打印语句
- 5.区块链平台以太坊从入门到精通之 以太网区块链网络
- Python 3从入门到精通6-if,elif,else语句
- Java学习从入门到精通
- maven学习-从入门到精通(一)