java.io.IOException Import; Import java.util.Enumeration; Import javax.servlet.ServletException; Import the javax.servlet.http.HttpServlet; Import the javax.servlet.http.HttpServletRequest; Import javax.servlet.http.HttpServletResponse; / * * * learning Object request: * role: request object currently mothballed all requested information * use: * get request header data * req.getMethod (); // get request method * req.getRequestURI (); // acquisition request URI of the * req.getRequestURL (); // get requests the URL of * req.getScheme (); // get agreement * * get the request line data * req.getHeader ( "key name"); // returns the specified request header information * req.getHeaderNames ( "User-Agent") ; // returns the requested head key name enumerations * * Req.getHeaderNames the Enumeration = E * (); the while (e.hasMoreElements ()) { String name = (String) e.nextElement (); String req.getHeader value2 = (name); System.out.println (e.nextElement () + ":" + name); * Get the user data * req.getParameter ( "keys"); // returns the specified user data * req.getParameterValues ( "keys"); // return the same value in different key the request data (multiple choice), returned array * req.getParameterNames (); // return all user requests to enumerate a collection of data * * Note: If you want to get the data request does not complain does not exist, return null * * Note : * when Tomcat server object is created by request, and passed as an argument to the service method of the servlet processing request. * * * @Author Administrator * * / public class RequestServlet the extends the HttpServlet { @Override // get all the data request line enumeration void-Service protected (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException { // Get request header data // Get request method String Method req.getMethod = (); System.out.println (Method); // get request the URL the StringBuffer = req.getRequestURL URL (); System.out.println (URL); // Get the URI String URI = req.getRequestURI (); System.out.println (URI); // Get request protocol String schem = req.getScheme (); System.out.println (Schem); // row data acquisition request String value = req.getHeader ( "the User-- Agent"); System.out.println (value); the Enumeration req.getHeaderNames E = (); the while (e.hasMoreElements ()) { String name = (String) e.nextElement (); String req.getHeader value2 = (name); System.out.println (e.nextElement () + ":" + name); // Get the user data String = req.getParameter NAME1 ( "the uname"); String pwd = req.getParameter ( "pwd"); System.out.println (NAME1 + ":" + pwd); // String FAV = REQ. getParameter ( "fav"); leak value extraction use an array String [] = favs req.getParameterValues ( "FAV"); IF (favs = null!) { for (String FAV: favs) { System.out.println ( FAV); } } // get the user requests an enumeration of all the keys of the data set req.getParameterNames ------ (); } } }
requestservlet.java
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MethodJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="req" method="post"> 用户名:<input type="text" name="uname" value=""><br /> 密码:<input type="password" name="pwd" value=""><br /> 爱好:<br /> <input type="checkbox" name="fav" value="1">唱歌<br /> <input type="checkbox" name="fav" value="2">跳舞<br /> <input type="checkbox" name="fav" value="3">泡妞<br /> <input type="submit" value="登录"> </form> </body> </html>
jsp page
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>servletlife</servlet-name> <servlet-class>com.sxt.in.servletlife</servlet-class> <load-on-startup>1</load-on-startup><!- load the server boot loading sequence in which 1 represents a stream of digital items -> <the servlet> </ the servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ServletMethod</servlet-name> <servlet-class>com.sxt.in.ServletMethod</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>RequestServlet</servlet-name> <servlet-class>com.sxt.in.RequestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletlife</servlet-name> <url-pattern>/life</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletMethod</servlet-name> <url-pattern>/method</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RequestServlet</servlet-name> <url-pattern>/req</url-pattern> </servlet-mapping> </web-app>
xml page