通过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