初学servlet2

    主要看看Request的一些方法:

 

public class TestRequest extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		int len = req.getContentLength();
		String contentType = req.getContentType();
		String localIP = req.getLocalAddr();
		String localName = req.getLocalName();
		int localPort = req.getLocalPort();

		String remoteIP = req.getRemoteAddr();
		String remoteHost = req.getRemoteHost();
		int remotePort = req.getRemotePort();

		String contextPath = req.getContextPath();
		String method = req.getMethod();
		String uri = req.getRequestURI();
		String queryString = req.getQueryString();

		resp.setContentType("text/html;charset=GB2312");

		PrintWriter out = resp.getWriter();
		out.println("<html><head><title>my title</title></head>");
		out.println("<body>");
		out.println("<br>请求正文内容长度:" + len);
		out.println("<br>请求正文的MIME类型:" + contentType);
		out.println("<br>服务器IP: " + localIP);
		out.println("<br>服务器名: " + localName);
		out.println("<br>服务器端口: " + localPort);
		out.println("<br>客户机IP: " + remoteIP);
		out.println("<br>客户机名: " + remoteHost);
		out.println("<br>客户机端口: " + remotePort);
		out.println("<br>客户请求的WEB应用的URL入口: " + contextPath);
		out.println("<br>客户请求的方法: " + method);
		out.println("<br>客户请求uri: " + uri);
		out.println("<br>查询字符串: " + queryString);

		out.println("</body></html>");

		out.flush();
		out.close();

	}

}

    把这个类放到合适的目录下,修改web.xml,访问,下图是显示结果:

初学servlet2

    这里并没有列出所有的Request和HttpServlet接口的方法,还有setAttribute,getParameter等方法。

你可能感兴趣的:(servlet)