您的位置:首页 > 其它

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源码:

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>

保存,即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: