通过request对象得到基本的信息

通过request得到一些基本的信息


package com.test;

import java.io.IOException;
import java.io.PrintWriter;

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

public class RequestInfoServlet extends HttpServlet {

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        process(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        process(request, response);
    }

    /**
     * 通过request对象得到基本的信息  
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void process(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        //项目的root 路径(全局路径)
        writer.write("request.getContextPath():" + request.getContextPath());
        writer.write("</br>");
        //得到servlet 在web.xml 里面申明的 url-pattern 路径
        writer.write("request.getServletPath():" + request.getServletPath());
        writer.write("</br>");
        //得到此servlet的完整路径 (包含根目录)
        writer.write("request.getRequestURI():" + request.getRequestURI());

        writer.write("</br>");
        //得到服务器的名称
        writer.write("request.getServerName():" + request.getServerName());
        writer.write("</br>");
        //如 HTTP/1.1
        writer.write("request.getProtocol():" + request.getProtocol());
        writer.write("</br>");
        //得到协议  如 HTTP
        writer.write("request.getScheme():" + request.getScheme());
        writer.write("</br>");
        //得到服务器的名称
        writer.write("request.getServerName():" + request.getServerName());
        writer.write("</br>");
        //得到端口号
        writer.write("request.getServerPort():" + request.getServerPort());
        writer.write("</br>");
        writer.close();

    }

    public void init() throws ServletException {
    }

}

-------------------------------------------------------------------输出------------------------------------------------------------

request.getContextPath():/song
request.getServletPath():/servlet/RequestInfoServlet
request.getRequestURI():/song/servlet/RequestInfoServlet
request.getServerName():localhost
request.getProtocol():HTTP/1.1
request.getScheme():http
request.getServerName():localhost
request.getServerPort():8080

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值