一.ServletRequest
1.获取参数的方法(四种)
> String getParameter(String name): 根据请求参数的名字, 返回参数值.
若请求参数有多个值(例如 checkbox), 该方法只能获取到第一个提交的值.
> String[] getParameterValues(String name): 根据请求参数的名字, 返回请求参数对应的字符串数组.
> Enumeration getParameterNames(): 返回参数名对应的 Enumeration 对象,
类似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法.
> Map getParameterMap(): 返回请求参数的键值对: key: 参数名, value: 参数值, String 数组类型.
2.程序indexhtml
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 <form action="login" method="post"> 9 user: <input type="text" name="user"/> 10 password: <input type="password" name="password"/><br><br> 11 12 interesting: 13 <input type="checkbox" name="interesting" value="reading"/>Reading 14 <input type="checkbox" name="interesting" value="game"/>Game 15 <input type="checkbox" name="interesting" value="party"/>Party 16 <input type="checkbox" name="interesting" value="shopping"/>Shopping 17 <input type="checkbox" name="interesting" value="sport"/>Sport 18 <input type="checkbox" name="interesting" value="tv"/>TV 19 20 <input type="submit" value="Submit"/> 21 </form> 22 </body> 23 </html>
3.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 <servlet> 7 <servlet-name>LoginServlet</servlet-name> 8 <servlet-class>servlets.LoginServlet</servlet-class> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>LoginServlet</servlet-name> 12 <url-pattern>/login</url-pattern> 13 </servlet-mapping> 14 </web-app>
4.LoginServlet.java
1 package servlets; 2 3 import java.io.IOException; 4 import java.util.Arrays; 5 import java.util.Enumeration; 6 import java.util.Map; 7 8 import javax.servlet.Servlet; 9 import javax.servlet.ServletConfig; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 14 public class LoginServlet implements Servlet{ 15 public void destroy() {} 16 public ServletConfig getServletConfig() { 17 return null; 18 } 19 public String getServletInfo() { 20 return null; 21 } 22 public void init(ServletConfig arg0) throws ServletException {} 23 public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { 24 //getParameter 25 String name=request.getParameter("user"); 26 System.out.println("user="+name); 27 System.out.println("========================="); 28 29 //getParameterValues 30 String[] names=request.getParameterValues("interesting"); 31 for(String inter:names) { 32 System.out.println("inter="+inter); 33 } 34 System.out.println("========================="); 35 36 //getParameterNames 37 Enumeration<String> en=request.getParameterNames(); 38 while(en.hasMoreElements()) { 39 String enName=en.nextElement(); 40 System.out.println("enName="+enName); 41 } 42 System.out.println("========================="); 43 44 //getParameterMap 45 Map<String,String[]> map=request.getParameterMap(); 46 for(Map.Entry<String,String[]> entry : map.entrySet()) { 47 System.out.println(entry.getKey()+"="+Arrays.asList(entry.getValue())); 48 } 49 50 } 51 52 }
5.效果
6.获取请求的 URI
7.获取请求方式
8.若是一个 GET 请求, 获取请求参数对应的那个字符串, 即 ? 后的那个字符串.
9.获取请求的 Serlvet 的映射路径
10.java程序
1 package servlets; 2 3 import java.io.IOException; 4 import java.util.Arrays; 5 import java.util.Enumeration; 6 import java.util.Map; 7 8 import javax.servlet.Servlet; 9 import javax.servlet.ServletConfig; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 import javax.servlet.http.HttpServletRequest; 14 15 public class LoginServlet implements Servlet{ 16 public void destroy() {} 17 public ServletConfig getServletConfig() { 18 return null; 19 } 20 public String getServletInfo() { 21 return null; 22 } 23 public void init(ServletConfig arg0) throws ServletException {} 24 public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { 25 // //getParameter 26 // String name=request.getParameter("user"); 27 // System.out.println("user="+name); 28 // System.out.println("========================="); 29 // 30 // //getParameterValues 31 // String[] names=request.getParameterValues("interesting"); 32 // for(String inter:names) { 33 // System.out.println("inter="+inter); 34 // } 35 // System.out.println("========================="); 36 // 37 // //getParameterNames 38 // Enumeration<String> en=request.getParameterNames(); 39 // while(en.hasMoreElements()) { 40 // String enName=en.nextElement(); 41 // System.out.println("enName="+enName); 42 // } 43 // System.out.println("========================="); 44 // 45 // //getParameterMap 46 // Map<String,String[]> map=request.getParameterMap(); 47 // for(Map.Entry<String,String[]> entry : map.entrySet()) { 48 // System.out.println(entry.getKey()+"="+Arrays.asList(entry.getValue())); 49 // } 50 51 //HttpServletRequest(负责http的Servlet) 52 HttpServletRequest hsr=(HttpServletRequest) request; 53 54 //getRequestURI 55 String requestUri=hsr.getRequestURI(); 56 System.out.println("requestUri="+requestUri); 57 System.out.println("========================="); 58 59 //getMethod 60 String method=hsr.getMethod(); 61 System.out.println("method="+method); 62 System.out.println("========================="); 63 64 //getQueryString 65 String queryString =hsr.getQueryString(); 66 System.out.println("queryString="+queryString); 67 System.out.println("========================="); 68 69 //getServletPath 70 String servletPath=hsr.getServletPath(); 71 System.out.println("servletPath="+servletPath); 72 System.out.println("========================="); 73 74 // 75 } 76 77 }
11.效果
二:response
1.将字放到html里
1 package servlets; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Arrays; 6 import java.util.Enumeration; 7 import java.util.Map; 8 9 import javax.servlet.Servlet; 10 import javax.servlet.ServletConfig; 11 import javax.servlet.ServletException; 12 import javax.servlet.ServletRequest; 13 import javax.servlet.ServletResponse; 14 import javax.servlet.http.HttpServletRequest; 15 16 public class LoginServlet implements Servlet{ 17 public void destroy() {} 18 public ServletConfig getServletConfig() { 19 return null; 20 } 21 public String getServletInfo() { 22 return null; 23 } 24 public void init(ServletConfig arg0) throws ServletException {} 25 public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { 26 // //getParameter 27 // String name=request.getParameter("user"); 28 // System.out.println("user="+name); 29 // System.out.println("========================="); 30 // 31 // //getParameterValues 32 // String[] names=request.getParameterValues("interesting"); 33 // for(String inter:names) { 34 // System.out.println("inter="+inter); 35 // } 36 // System.out.println("========================="); 37 // 38 // //getParameterNames 39 // Enumeration<String> en=request.getParameterNames(); 40 // while(en.hasMoreElements()) { 41 // String enName=en.nextElement(); 42 // System.out.println("enName="+enName); 43 // } 44 // System.out.println("========================="); 45 // 46 // //getParameterMap 47 // Map<String,String[]> map=request.getParameterMap(); 48 // for(Map.Entry<String,String[]> entry : map.entrySet()) { 49 // System.out.println(entry.getKey()+"="+Arrays.asList(entry.getValue())); 50 // } 51 //******************************************************************// 52 // //HttpServletRequest(负责http的Servlet) 53 // HttpServletRequest hsr=(HttpServletRequest) request; 54 // 55 // //getRequestURI 56 // String requestUri=hsr.getRequestURI(); 57 // System.out.println("requestUri="+requestUri); 58 // System.out.println("========================="); 59 // 60 // //getMethod 61 // String method=hsr.getMethod(); 62 // System.out.println("method="+method); 63 // System.out.println("========================="); 64 // 65 // //getQueryString 66 // String queryString =hsr.getQueryString(); 67 // System.out.println("queryString="+queryString); 68 // System.out.println("========================="); 69 // 70 // //getServletPath 71 // String servletPath=hsr.getServletPath(); 72 // System.out.println("servletPath="+servletPath); 73 // System.out.println("========================="); 74 //******************************************************************// 75 //response 76 // response.setContentType("text/html"); 77 // response.setContentType("text/msdoc"); 78 PrintWriter out = response.getWriter(); 79 out.println("helloworld..."); 80 } 81 82 }
2.效果
时间: 2024-10-27 01:56:32