码迷,mamicode.com
首页 > Web开发 > 详细

Servlet 之 HttpServlet

时间:2016-01-11 23:41:03      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

package cn.jiemoxiaodi.http;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyHttpServlet extends GenericServlet {

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // 如果我们确定请求的是用http协议,那么实际上ServletRequest 就是
        // HttpServletRequest 所以进行强转

        HttpServletRequest request = (HttpServletRequest) arg0;

        HttpServletResponse response = (HttpServletResponse) arg1;
        
        
        //当我们请求servlet的时候 ,如果他请求方式是get 那么他走一个doGet方法
        //当我们请求servlet时候,如果他请求方式是post  那么他走一个doPost的方法
        
        //获得 请求的 方式
        String method= request.getMethod();
        if(method.equalsIgnoreCase("get")){
            doGet(request,response);
        }else if(method.equalsIgnoreCase("post")){
            doPost(request,response);
        }
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("dopost");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("doget");
    }

}

 

package cn.jiemoxiaodi.http;

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

public class DServlet extends MyHttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("dopost----=--");
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("doget----=--");
    }

}

1.关于Servlet的线程安全问题
        因为一个servlet的实例在服务器中只存在一份. ==> 当有多个请求访问时,servlet线程将不安全.
        如何规避线程安全问题呢? ===>  我们接受线程的信息时,将信息保存在方法的变量中,不要使用成员变量保存.

    2.关于servlet中路径配置问题
            <!--
                  路径配置分为两种情况
                    路径匹配
                        /DServlet   ==>  http://localhost:8080/Day10/DServlet
                        /ABC/DServlet  ==> http://localhost:8080/Day10/ABC/DServlet
                        /ABC/BCD/DServlet ==> http://localhost:8080/Day10/ABC/BCD/DServlet
                        /ABC/*  ==>  http://localhost:8080/Day10/ABC/asdlkajsdlkjalsd
                        /       ==> 匹配所有路径 http://localhost:8080/Day10/2139009123j/asd2klnasd
                        匹配优先级: 路径匹配中 ,匹配的范围越广,优先级越低.
                    后缀名匹配
                        *.do        ==>  http://localhost:8080/Day10/213oiajsdoijoad.do
                        *.action    ==>  http://localhost:8080/Day10/lnasdljasdlkasljdasd.action
                        *.abc        ==>   http://localhost:8080/Day10/1039i09ixc0kasd.abc
                    这种路径匹配在现在学习阶段接触不到. 在学到Filter 和struts2 的时候就用到了.
                    !!!!!!!注意: 以上两种匹配方式不能同时使用
                            例如下面是绝对错误的:
                                /DServlet/*.do  ==> 凡事路径配置中以"/"开头那么就说明含有路径匹配.这种时候再使用*.do 这种后缀名匹配绝对报错.
                   -->

Servlet 之 HttpServlet

标签:

原文地址:http://www.cnblogs.com/jiemoxiaodi/p/5122823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!