?? showrequestheaders.java
字號:
package eg.cha5;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.ServletUtilities;
public class ShowRequestHeaders extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String title = "顯示所有請求頭";
StringBuffer content = new StringBuffer();
content.append(ServletUtilities.headWithTitle(title));
content.append("<body bgcolor=\"#fdf5e6\">\n");
content.append("<h1 align=center>").append(title).append("</h1>\n");
content.append("<b>Request Method: </b>").append(request.getMethod()).append("<br>\n");
content.append("<b>Request URI: </b>").append(request.getRequestURI()).append("<br>\n");
content.append("<b>Request Protocol: </b>").append(request.getProtocol()).append("<br><br>\n");
content.append("<table border=1 align=center>\n");
content.append("<tr bgcolor=\"#ffad00\">\n");
content.append("<th>header name</th>\n");
content.append("<th>header value</th>\n");
content.append("</tr>\n");
Enumeration<?> headernames = request.getHeaderNames();
while (headernames.hasMoreElements()) {
String headername = (String) headernames.nextElement();
content.append("<tr>\n");
content.append("<td>").append(headername).append("</td>\n");
content.append("<td>").append(request.getHeader(headername)).append("</td>\n");
content.append("</tr>\n");
}
content.append("</table>\n");
content.append("</body></html>");
out.write(content.toString());
//后面的代碼以后有用
// request.setAttribute("User", "Bily");
//
// RequestDispatcher
// requestDispatcher=request.getRequestDispatcher("/index.jsp");
// requestDispatcher.forward(request,response);
// response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -