一.ServletRequest
1.获取参数的方法(四种)
> String getParameter(String name): 根据请求参数的名字, 返回参数值.
若请求参数有多个值(例如 checkbox), 该方法只能获取到第一个提交的值.
> String[] getParameterValues(String name): 根据请求参数的名字, 返回请求参数对应的字符串数组.
> Enumeration getParameterNames(): 返回参数名对应的 Enumeration 对象,
类似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法.
> Map getParameterMap(): 返回请求参数的键值对: key: 参数名, value: 参数值, String 数组类型.
2.程序indexhtml
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
user: <input type="text" name="user"/>
password: <input type="password" name="password"/><br><br> interesting:
<input type="checkbox" name="interesting" value="reading"/>Reading
<input type="checkbox" name="interesting" value="game"/>Game
<input type="checkbox" name="interesting" value="party"/>Party
<input type="checkbox" name="interesting" value="shopping"/>Shopping
<input type="checkbox" name="interesting" value="sport"/>Sport
<input type="checkbox" name="interesting" value="tv"/>TV <input type="submit" value="Submit"/>
</form>
</body>
</html>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
4.LoginServlet.java
package servlets; import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class LoginServlet implements Servlet{
public void destroy() {}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
//getParameter
String name=request.getParameter("user");
System.out.println("user="+name);
System.out.println("========================="); //getParameterValues
String[] names=request.getParameterValues("interesting");
for(String inter:names) {
System.out.println("inter="+inter);
}
System.out.println("========================="); //getParameterNames
Enumeration<String> en=request.getParameterNames();
while(en.hasMoreElements()) {
String enName=en.nextElement();
System.out.println("enName="+enName);
}
System.out.println("========================="); //getParameterMap
Map<String,String[]> map=request.getParameterMap();
for(Map.Entry<String,String[]> entry : map.entrySet()) {
System.out.println(entry.getKey()+"="+Arrays.asList(entry.getValue()));
} } }
5.效果
6.获取请求的 URI
7.获取请求方式
8.若是一个 GET 请求, 获取请求参数对应的那个字符串, 即 ? 后的那个字符串.
9.获取请求的 Serlvet 的映射路径
10.java程序
package servlets; import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; public class LoginServlet implements Servlet{
public void destroy() {}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// //getParameter
// String name=request.getParameter("user");
// System.out.println("user="+name);
// System.out.println("=========================");
//
// //getParameterValues
// String[] names=request.getParameterValues("interesting");
// for(String inter:names) {
// System.out.println("inter="+inter);
// }
// System.out.println("=========================");
//
// //getParameterNames
// Enumeration<String> en=request.getParameterNames();
// while(en.hasMoreElements()) {
// String enName=en.nextElement();
// System.out.println("enName="+enName);
// }
// System.out.println("=========================");
//
// //getParameterMap
// Map<String,String[]> map=request.getParameterMap();
// for(Map.Entry<String,String[]> entry : map.entrySet()) {
// System.out.println(entry.getKey()+"="+Arrays.asList(entry.getValue()));
// } //HttpServletRequest(负责http的Servlet)
HttpServletRequest hsr=(HttpServletRequest) request; //getRequestURI
String requestUri=hsr.getRequestURI();
System.out.println("requestUri="+requestUri);
System.out.println("========================="); //getMethod
String method=hsr.getMethod();
System.out.println("method="+method);
System.out.println("========================="); //getQueryString
String queryString =hsr.getQueryString();
System.out.println("queryString="+queryString);
System.out.println("========================="); //getServletPath
String servletPath=hsr.getServletPath();
System.out.println("servletPath="+servletPath);
System.out.println("========================="); //
} }
11.效果
二:response
1.将字放到html里
package servlets; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; public class LoginServlet implements Servlet{
public void destroy() {}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException {}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// //getParameter
// String name=request.getParameter("user");
// System.out.println("user="+name);
// System.out.println("=========================");
//
// //getParameterValues
// String[] names=request.getParameterValues("interesting");
// for(String inter:names) {
// System.out.println("inter="+inter);
// }
// System.out.println("=========================");
//
// //getParameterNames
// Enumeration<String> en=request.getParameterNames();
// while(en.hasMoreElements()) {
// String enName=en.nextElement();
// System.out.println("enName="+enName);
// }
// System.out.println("=========================");
//
// //getParameterMap
// Map<String,String[]> map=request.getParameterMap();
// for(Map.Entry<String,String[]> entry : map.entrySet()) {
// System.out.println(entry.getKey()+"="+Arrays.asList(entry.getValue()));
// }
//******************************************************************//
// //HttpServletRequest(负责http的Servlet)
// HttpServletRequest hsr=(HttpServletRequest) request;
//
// //getRequestURI
// String requestUri=hsr.getRequestURI();
// System.out.println("requestUri="+requestUri);
// System.out.println("=========================");
//
// //getMethod
// String method=hsr.getMethod();
// System.out.println("method="+method);
// System.out.println("=========================");
//
// //getQueryString
// String queryString =hsr.getQueryString();
// System.out.println("queryString="+queryString);
// System.out.println("=========================");
//
// //getServletPath
// String servletPath=hsr.getServletPath();
// System.out.println("servletPath="+servletPath);
// System.out.println("=========================");
//******************************************************************//
//response
// response.setContentType("text/html");
// response.setContentType("text/msdoc");
PrintWriter out = response.getWriter();
out.println("helloworld...");
} }
2.效果