1.HttpServletResponse
如果说DOM是javascript与HTML的桥梁,那么servlet就是前端与后端的桥梁,HttpServletRequest和HttpServletResponse就是之间的信使。
1.1请求行:
response. sendRedirect(String location) 请求重定向
等同于:
响应行---response. setStatus(int sc) 设置响应状态码,如302,告诉客户端要重新定向资源
响应头---response. setHeader(String name, String value) 设置响应头信息,告诉浏览器要去访问的URL
2.HttpServletRequest
2.1请求行
★ Get请求: http://localhost:8080/day09/servlet/req1?username=zs http/1.1
★ getMethod(); 获得请求方式
★ getRequestURL();返回客户端发出请求时的完整URL。 http://localhost:8080/day09/servlet/req1
★ getRequestURI(); 返回请求行中的资源名部分。/项目名后的路径:/servlet/req1
★ getContextPath(); 当前应用的虚拟目录。/项目名:/day09
★ getQueryString() ; 返回请求行中的参数部分。username=zs
2.2请求消息头
★ String getHeader(String name) 根据头名称得到头信息值
★ Enumeration getHeaderNames() 得到所有头信息name
★ Enumeration getHeaders(String name) 根据头名称得到相同名称头信息值
2.3Servlet获取表单数据的方法
★ 单个请求参数的读取 调用HttpServletRequest的getParameter方法 ,返回与指定参数相应的值。参数区分大小写。参数没有相应的值则返回空String, 如果没有该参数则返回null。 对于多个同一参数名则返回首次出现的值。
获取表单数据
getParameter(String name) 通过名称获取表单数据
getParameterValues(String name) 提取复选框的方法
getParameterNames() 获取表单所有名称
getParameterMap() 获取表单提交的所有值
getInputStream() 获取表单字节流
注意:
getParameterValues方法,返回指定参数所有相应的值,返回类型为字符串数组。
getParameterNames和getParameterMap方法对参数名进行查找。返回的参数名不会按表单中的次序排列。
注册表单:
register.html:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/webService/ServletDemo9" method="post">
用户名:<input type="text" name="username" /><br /> 密码:<input
type="password" name="password" /><br />
<!--不能输入文字的,为了使后台接收文字,需要用到value属性 ;注释快捷键Ctrl+shift+c-->
性别:<input type="radio" name="sex" value="男" checked="checked" />男 <input
type="radio" name="sex" value="女" />女<br />
爱好:
<input type="checkbox"name="hobby" value="篮球" />篮球
<input type="checkbox"name="hobby" value="唱歌"/>唱歌
<input type="checkbox"name="hobby" value="跳舞"/>跳舞
<input type="checkbox"name="hobby" value="看书"/>看书
<input type="checkbox"name="hobby" value="登山"/>登山
</br>
所在城市:
<select name="city">
<option >请选择</option>
<option value="bj" >北京</option>
<option value="bj">北京1</option>
<option value="bj">北京2</option>
<option value="bj">北京3</option>
</select>
</br>
<input type="submit" value= "注册">
</form>
</body>
</html>
方法一:直接通过找名字来获取值
ServletDemo9.java:
package com.cms.Demo1.Service;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo9
*/
public class ServletDemo9 extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受到表单的数据, 先使用request设置服务器应该使用的字符编码,否则字符不统一会出现乱码
request.setCharacterEncoding("UTF-8");
//根据表单中的name属性名,获取value属性值的方法
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String city = request.getParameter("city");
//数据有多个,用数组方式获得
String[] hobbys = request.getParameterValues("hobby");
System.out.println( username );
System.out.println( password);
System.out.println(sex);
System.out.println(city);
//hobbys不选的话默认值为空,不能输出任何结果
for (int i = 0;hobbys!=null && i < hobbys.length; i++) {
System.out.println(hobbys[i]);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
方法二:
上一种是通过请求对象一个一个的获取其值,而这种法子是先将名字全部获取到,在通过循环获取取出表单类型的值。
package com.cms.Demo1.Service;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo10 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取表单所有name的名字
Enumeration<String> names = request.getParameterNames();
//枚举
while (names.hasMoreElements()) {
String name = (String) names.nextElement();//得到每一个name名
System.out.println(name);
String[] values = request.getParameterValues(name);//得到name的value值
//循环输出
for (int i = 0; values!=null&&i < values.length; i++) {
System.out.println(values[i]);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
方法三:
方法一、二在控制台打印输出只是为了检验结果,最终的目的都是为了将获取到的表单数据插入到对应的数据库当中。这里我们就把将数据封装到类中。
创建用户类(字段要和表单名一致)
第一步:导入所需jar包
第二步:创建用户实体类(字段要和表单名一致)---
UserInformation.java 类文件
package com.cms.Demo1.entity;
import java.util.Arrays;
import org.apache.catalina.connector.Request;
public class UserInformation {
//变量名与register.html表单的name一致.约定优于编码
private String username;
private String password;
private String sex;
private String[] hobby;
private String city;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
//整合数据,重写toString方法
@Override
public String toString() {
return "UserInformation [username=" + username + ", password=" + password + ", sex=" + sex + ", hobby="
+ Arrays.toString(hobby) + ", city=" + city + "]";
}
}
第三步:编写Servlet文件
package com.cms.Demo1.Service;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.cms.Demo1.entity.UserInformation;
public class ServletDemo9 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// test(request);
try {
request.setCharacterEncoding("UTF-8");
UserInformation u=new UserInformation();
System.out.println("封装数据前:"+u);
BeanUtils.populate(u, request.getParameterMap());
System.out.println("封装数据后:"+u);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
结果:
与请求编码相关的方法:
//解决post方式编码,让请求和响应,页面和Servlet的输出的字符编码与预期一致。
*****request.setCharacterEncoding("UTF-8"); //告诉服务器客户端什么编码,只能处理post请求方式
//解决get方式编码, 将数据提交给String构造器,用指定字符集转换数据
String name = new String(name.getBytes(“iso-8859-1”),”UTF-8”);
注意: