欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javaWeb HttpServletRequest接口。

程序员文章站 2024-02-04 14:14:40
...

请求行:
 

package com.my.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ContextServlet
 */
@WebServlet("/ContextServlet")
public class ContextServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContextServlet() {
        super();
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		
		String method = request.getMethod();//获取使用的HTTP方法;
		
		String URi = request.getRequestURI();//获取请求中的资源名部分
		
		String protocol = request.getProtocol();//获得协议号
		
		String queryString = request.getQueryString();//获得查询语句
		
		String servletPath = request.getServletPath();
		
		String contextpath = request.getContextPath();//获得web应用路径
		
		PrintWriter out = response.getWriter();
		
		out.println("<p>请求使用的http方法"+method+"</p>");
		
		out.println("<p>请求行中的资源名部分"+URi+"</p>");
		
		out.println("<p>请求使用的协议"+protocol+"</p>");
		
		out.println("<p>查询字符串"+queryString+"</p>");

		out.println("<p>Servlet所映射的路径"+servletPath+"</p>");
		
		out.println("<p>请求资源所属的WEb应用路径"+contextpath+"</p>");
	
		out.flush();

		out.close();
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);

	}

}