1. 程式人生 > >Servlet獲取Request和Response

Servlet獲取Request和Response

分析Request

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

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

/**
 * Interface
 *      servlet容器(伺服器)建立了這個物件,和伺服器耦合性高
 * 為了讓解析請求資訊更加的方便
 *      行
 *      頭
 *      網
 *      體       
 */
public class HiServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取請求行資訊 getRequestLine(request); //獲取請求頭資訊 getRequestHeader(request); //獲取網路資訊資訊
getRequestNet(request); //獲取請求體資訊 getRequestBody(request); } /** * 獲取請求行資訊 * @param request */ private void getRequestLine(HttpServletRequest request) { System.out.println("請求方式" + request.getMethod()); System.out.println("資源路徑url"
+ request.getRequestURL()); System.out.println("資源路徑uri" + request.getRequestURI()); System.out.println("協議" + request.getScheme()); System.out.println("協議版本" + request.getProtocol()); System.out.println("專案名稱" + request.getContextPath()); } /** * 獲取請求頭資訊 * @param request */ private void getRequestHeader(HttpServletRequest request) { //獲取指定的請求頭資訊 System.out.println("User-Agent:" + request.getHeader("User-Agent")); System.out.println("User-Agent:" + request.getHeader("User-Agent".toLowerCase()));//大小寫無影響 System.out.println("User-Agent:" + request.getHeader("User-Agent".toUpperCase())); //獲取所有的請求頭資訊 Enumeration<String> headerNames = request.getHeaderNames(); //遍歷所有的名字 while (headerNames.hasMoreElements()) { //獲取請求頭的名字 String headerName = headerNames.nextElement(); //獲取名字對應的值 System.out.println(headerName + ":" + request.getHeader(headerName)); } } /** * 獲取網路資訊 * @param request */ private void getRequestNet(HttpServletRequest request) { System.out.println("伺服器資訊:" + request.getLocalAddr() + ":" + request.getLocalPort()); System.out.println("客戶端資訊:" + request.getRemoteAddr() + ":" + request.getRemotePort()); } /** * 獲取請求引數 * @param request */ private void getRequestBody(HttpServletRequest request) { //獲取請求引數對應的值 System.out.println("使用者名稱" + request.getParameter("uname"));//text System.out.println("密碼" + request.getParameter("pwd"));//password System.out.println("電子郵箱" + request.getParameter("email"));//text System.out.println("年齡" + request.getParameter("age"));//text System.out.println("真實姓名" + request.getParameter("realname"));//text System.out.println("性別" + request.getParameter("gender"));//radio //一對多的時候 String[] favs = request.getParameterValues("fav");//checkbox //判斷是否為空 if (favs != null && favs.length >= 1) { //遍歷 for (int i = 0; i < favs.length; i++) { System.out.println(favs[i]); } } } }

分析Response

import java.io.IOException;

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

/**
 * 伺服器建立了這個物件
 *      行
 *      頭
 *      體
 * @author Administrator
 *
 */
public class HiServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HiServlet.service()");
        //響應行(一般不需要程式設計師手動操作
        //      resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
        //響應頭
        resp.setHeader("Server", "Tomcat");
        //不能設定同名的值,新值會覆蓋掉上一個值
        resp.setHeader("hello", "bjsxt");
        resp.setHeader("hello", "sxt");
        //允許多個同名的值
        resp.addHeader("hi", "hanmeimei");
        resp.addHeader("hi", "lilei");
        //設定mime型別(可以解決中文亂碼)
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("content-type", "text/html;charset=utf-8");
        //響應實體內容
        resp.getWriter().print("<h1>hello <font onclick='alert(1234567890);' color='red'>漢字</font>  jin liang bu yao chu xian zhong wen" + Math.random() + " </h1>");
    }
}