一、HttpServletRequest概述:
我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和代表响应response。
service方法中的request的类型是ServletRequest,而doGet/doPost方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口。
二、通过抓包工具抓取Http请求:
三、通过request获得请求行:
String getRequestURI():
StringBuffer getRequestURL():
String getContextPath():web应用的名称
String getQueryString() :提交url地址后的参数字符串
request.getRemoteAddr() :获得客户机(客户端)的一些信息
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获得请求方式
String method = request.getMethod();
System.out.println("method:"+method);
//2、获得请求的资源相关的内容
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println("requestURI:"+requestURI);
System.out.println("requestURL:"+requestURL);
//3.获得web应用的名称
String contextPath = request.getContextPath();
System.out.println("web应用:"+contextPath);
//4.地址后的参数的字符串
String queryString = request.getQueryString();
System.out.println(queryString);
//5、获得客户机的信息---获得访问者IP地址
String remoteAddr = request.getRemoteAddr();
System.out.println("IP:"+remoteAddr);
}
四、通过request获得请求头:
long getDateHeader(String name)
String getHeader(String name)
Enumeration getHeaderNames()
Enumeration getHeaders(String name)
int getIntHeader(String name)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获得指定的头
String header = request.getHeader("User-Agent");
System.out.println(header);
//2、获得所有的头的名称
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
System.out.println(headerName+":"+headerValue);
}
}
referer头的作用:执行该此访问的的来源,可以用做防盗链:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//对该新闻的来源的进行判断
String header = request.getHeader("referer");
if(header!=null&&header.startsWith("http://localhost")){
//是从我自己的网站跳转过来的 可以看新闻
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("中国确实已经拿到100块金牌....");
}else{
response.getWriter().write("你是盗链者,可耻!!");
}
}
五、通过request获得请求体:
**String getParameter(String name) **
String[] getParameterValues(String name)
Enumeration getParameterNames()
Map<String,String[]> getParameterMap()
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获得单个表单值
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
System.out.println("------------------");
//2、获得多个表单的值
String[] hobbys = request.getParameterValues("hobby");
for (String hobby : hobbys) {
System.out.println(hobby);
}
System.out.println("------------------");
//3、获得所有的请求参数的名称
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
System.out.println(parameterNames.nextElement());
}
System.out.println("------------------");
//4、获得所有的参数 参数封装到一个Map<String,String[]>
Map<String, String[]> parameterMap = request.getParameterMap();
for(Map.Entry<String, String[]> entry:parameterMap.entrySet()){
System.out.println(entry.getKey());
for(String str:entry.getValue()){
System.out.println(str);
}
System.out.println("-----------------------------------");
}
}
解决post提交方式的乱码:
request.setCharacterEncoding("UTF-8");
解决get提交的方式的乱码:
parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");
六、request的其他功能:
(1)request是一个域对象:
request对象也是一个存储数据的区域对象,所以也具有如下方法:
setAttribute(String name, Object o)
getAttribute(String name)
removeAttribute(String name)
(2)request完成请求转发:
获得请求转发器----path是转发的地址
RequestDispatcher getRequestDispatcher(String path)
通过转发器对象转发
requestDispathcer.forward(ServletRequest request, ServletResponse response)
案例实现:
两个servlet:
Servlet1如下:
package com.forward;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//想request域中存储数据
request.setAttribute("name", "tom");
//servlet1 将请求转发给servlet2
RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet2");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Servlet2如下:
package com.forward;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object attribute = request.getAttribute("name");
response.getWriter().write("hello haohao..."+attribute);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
注册功能案例实现:
第一步:环境搭建:
1.导入jar包
2.导入c3p0配置文件到src下;
3.导入utils工具类;
4.添加User实体类
代码实现如下:
主要是RegisterServlet类:
package com.ithiema.register;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;
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 org.apache.commons.dbutils.QueryRunner;
import com.zl.utils.DataSourceUtils;
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置request的编码---只适合post方式
request.setCharacterEncoding("UTF-8");
//get方式乱码解决
//String username = request.getParameter("username");//乱码
//先用iso8859-1编码 在使用utf-8解码
//username = new String(username.getBytes("iso8859-1"),"UTF-8");
//1、获取数据
//String username = request.getParameter("username");
//System.out.println(username);
//String password = request.getParameter("password");
//.....
//2、将散装的封装到javaBean
//User user = new User();
//user.setUsername(username);
//user.setPassword(password);
//使用BeanUtils进行自动映射封装
//BeanUtils工作原理:将map中的数据 根据key与实体的属性的对应关系封装
//只要key的名字与实体的属性 的名字一样 就自动封装到实体中
Map<String, String[]> properties = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user, properties);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//现在这个位置 user对象已经封装好了
//手动封装uid----uuid---随机不重复的字符串32位--java代码生成后是36位
user.setUid(UUID.randomUUID().toString());
//3、将参数传递给一个业务操作方法
try {
regist(user);
} catch (SQLException e) {
e.printStackTrace();
}
//4、认为注册成功跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
//注册的方法
public void regist(User user) throws SQLException{
//操作数据库
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(),
user.getEmail(),null,user.getBirthday(),user.getSex(),null,null);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
表结构如下:
ServletContext域与Request域的生命周期比较?
ServletContext:
创建:服务器启动
销毁:服务器关闭
域的作用范围:整个web应用
request:
创建:访问时创建request
销毁:响应结束request销毁
域的作用范围:一次请求中
转发与重定向的区别?
1)重定向两次请求,转发一次请求
2)重定向地址栏的地址变化,转发地址不变
3)重新定向可以访问外部网站 转发只能访问内部资源
4)转发的性能要优于重定向
用户登录失败信息提示:
package com.ithiema.login;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.zl.register.User;
import com.zl.utils.DataSourceUtils;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//1、获得用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//2、调用一个业务方法进行该用户查询
User login = null;
try {
login = login(username,password);
} catch (SQLException e) {
e.printStackTrace();
}
//3、通过user是否为null判断用户名和密码是否正确
if(login!=null){
//用户名和密码正确
//登录成功 跳转到网站的首页
response.sendRedirect(request.getContextPath());
}else{
//用户名或密码错误
//跳回当前login.jsp
//使用转发 转发到login.jsp 向request域中存储错误信息
request.setAttribute("loginInfo", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
public User login(String username,String password) throws SQLException{
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username=? and password=?";
User user = runner.query(sql, new BeanHandler<User>(User.class), username,password);
return user;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
在login.jsp的表单上方加一句下面的代码:
<div><%=request.getAttribute("loginInfo")==null?" ":request.getAttribute("loginInfo")%></div>