⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showrequestheaders.java

📁 servlet的示例代码
💻 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 + -