Servlet从入门到精通三

 

一、HttpServlet和一些开发细节

Servlet接口与两个默认实现类:GenericServlet、HttpServlet

1、HttpServlet指能够处理HTTP请求的Servlet,它在原有Servlet接口上添加了一些与HTTP协议处理方法,它比Servlet接口的功能更为强大。因此开发人员在编写Servlet时,通常应该继承这个类,而避免直接去实现Servlet接口。

2、HttpServlet在实现Servlet接口时,覆写了service()方法,改方法体内的代码会自动判断用户的请求方式,如为GET请求,则调用HttpServletdoGet方法,如为Post请求,则调用doPost方法。因此,开发人员在编写Servlet时,通常只需要覆写doGetdoPost方法,而不要去覆写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>

保存,即可

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值