Eclipse部署JavaWeb项目完整流程(含代码:获取http请求头)

一.Eclipse集成Tomcat

二.创建Javaweb项目

 

File--New--Dynamic Web Project

 next-next

 Finish!

 

三.部署web项目到Tomcat上

 项目右击New--Servlet

 Finish以后,会自动生成以下代码

 修改doGet()方法

String ua = request.getHeader("User-Agent");
System.out.println("User-Agent: " + ua);

  完整代码如下:

import java.io.IOException;
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 GetHttpHeader
 */
@WebServlet("/GetHttpHeader")
public class GetHttpHeader extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetHttpHeader() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		String ua = request.getHeader("User-Agent");
        System.out.println("User-Agent: " + ua);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

在左侧Java文件上右击,Run As-->Run on server

 

 添加到右边,表示项目发布到服务器(左侧表示未发布的,右侧表示发布)

 

 Add添加至右边后,点击Finish后,直接运行

运行结果,控制台显示如下(同时浏览器会自动打开网址http://localhost:8081/test/GetHttpHeader)

 

运行后默认打开浏览器,然后在控制台输出相关信息。

也可以不打开浏览器,运行后通过手动打开浏览器输入地址后http://localhost:8081/test/GetHttpHeader,或者http://127.0.0.1:8080/test/GetHttpHeader 才在控制台输出相关信息:

步骤如下:java文件右击--Run As-->Run Configurations

 

 

 

 -------------------------------------------------------------

获取HTTP所有请求头信息(同样修改doGet()方法即可)

完整代码如下:

import java.io.IOException;
import java.util.Enumeration;

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 GetHttpHeader
 */
@WebServlet("/GetHttpHeader")
public class GetHttpHeader extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetHttpHeader() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		System.out.println("-----------获取单个请求头信息-----");
		String ua = request.getHeader("User-Agent");
		System.out.println("User-Agent: " + ua);
		System.out.println("-----------获取所有请求头信息-----");
		Enumeration<String> headers = request.getHeaderNames();
		while (headers.hasMoreElements()) {
		    String headerName = headers.nextElement();
		    String headerValue = request.getHeader(headerName);
		    System.out.println(headerName + ": " + headerValue);
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  运行结果

 

--------------------------------其他信息获取-----------------------------------

 

 

        System.out.println("浏览器基本信息:"+request.getHeader("user-agent"));
        System.out.println("客户端系统名称:"+System.getProperty("os.name"));
        System.out.println("客户端系统版本:"+System.getProperty("os.version"));
        System.out.println("客户端操作系统位数:"+System.getProperty("os.arch"));
        System.out.println("HTTP协议版本:"+request.getProtocol());
        System.out.println("请求编码格式:"+request.getCharacterEncoding());
        System.out.println("Accept:"+request.getHeader("Accept"));
        System.out.println("Accept-语言:"+request.getHeader("Accept-Language"));
        System.out.println("Accept-编码:"+request.getHeader("Accept-Encoding"));
        System.out.println("Connection:"+request.getHeader("Connection"));
        System.out.println("Cookie:"+request.getHeader("Cookie"));
        System.out.println("客户端发出请求时的完整URL"+request.getRequestURL());
        System.out.println("请求行中的资源名部分"+request.getRequestURI());
        System.out.println("请求行中的参数部分"+request.getRemoteAddr());
        System.out.println("客户机所使用的网络端口号"+request.getRemotePort());
        System.out.println("WEB服务器的IP地址"+request.getLocalAddr());
        System.out.println("WEB服务器的主机名"+request.getLocalName());
        System.out.println("客户机请求方式"+request.getMethod());
        System.out.println("请求的文件的路径"+request.getServerName());
        System.out.println("请求体的数据流"+request.getReader());
        BufferedReader br=request.getReader();
        String res = "";
        while ((res = br.readLine()) != null) {
        System.out.println("request body:" + res);
        }
        System.out.println("请求所使用的协议名称"+request.getProtocol());
        System.out.println("请求中所有参数的名字"+request.getParameterNames());
        Enumeration enumNames= request.getParameterNames();
        while (enumNames.hasMoreElements()) {
        String key = (String) enumNames.nextElement();
        System.out.println("参数名称:"+key);

  

  

posted @ 2023-08-28 15:01  yimu-yimu  阅读(93)  评论(0编辑  收藏  举报