request请求
- request 请求
- index.jsp
- login.jsp
- success.jsp
- LoginServlet
- SuccessServlet
- 响应写入用户名和密码测试
- 请求转发success.jsp页面测试
- 请求转发SuccessServlet 页面测试
- 重定向SuccessServlet 页面测试
request 请求
作用:获取浏览器发送过来的数据
组成部分:请求行 请求头 请求体
操作请求行:
格式:
请求方式 请求资源 协议/版本
GET /xxxweb/row?username=dyj&password=111 HTTP/1.1
常用方法:HttpServletRequest
掌握:
String getMethod():获取请求方式
String getRemoteAddr():获取ip地址
String getContextPath() :获取当前web项目名称(如:/loginweb)
了解:
getRequestURI():获取从项目名到参数之前的内容 /xxxweb/login
getRequestURL():获取带协议的完整路径 http://localhost/xxxweb/login
String getQueryString():get请求的所有参数 username=djy&password=123
String getProtocol():获取协议和版本
操作请求头(了解):
格式:key/value( value 可以是多个值)
方法:
了解:
String getHeader(String key):通过 key 获取指定的 value (一个)
通过 getHeaders(String name):通过 key 获取指定的 value (多个)
通过 request.getHeaderNames() :获取所有的请求头的名称(先 request 设置响应头)
int getIntHeader(String key):获取整型的请求头
long getDateHeader(String key):获取时间的请求头
请求头:
user-agent:浏览器内核 msie firefox chrome
referer:页面从那里来(防盗链)
操作请求参数(掌握):
username=djy&password=123&hobby=eat
常用方法:
String getParameter(String key):获取一个值(掌握)
String[] getParameterValues(String key):通过一个 key 获取多个值(了解)
Map<String,String[]> getParameterMap():获取所有的参数名称和值(了解)
请求的中文乱码:
request.setCharacterEncoding(“utf-8”);
index.jsp
<a href="login.jsp">登录</a>
login.jsp
<form action="<%=request.getContextPath()%>/login" method="post"><label>用户名:</label><input type="text" name="userName"><br><label>密码:</label><input type="password" name="password"><br><button type="submit">提交</button><label>||</label><button type="reset">重置</button></form>
success.jsp
<h1>登录成功</h1><h2>请求域数据对象,从Java代码中拿到</h2><hr>登录名:<%=request.getAttribute("user")%><br>ip: <%=request.getAttribute("ip")%>
LoginServlet
@WebServlet(name = "login", value = "/login")
public class LoginServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置请求响应编码格式为utf-8request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=UTF-8");//请求拿到用户名和密码String userName = request.getParameter("userName");String password = request.getParameter("password");//请求ip地址//String remoteAddr = request.getRemoteAddr();String localAddr = request.getLocalAddr();//请求协议和版本String protocol = request.getProtocol();//响应写入用户名和密码(把下面代码注释显示数据)response.getWriter().append("用户名:" + userName).append("<br>").append("密码:" + password).append("<br>").append("ip地址:" + localAddr).append("<br>").append("协议和版本:" + protocol);//设置域对象(给success页面中传递数据)request.setAttribute("user",userName);request.setAttribute("ip",localAddr);//请求转发//请求转发(地址不改变,只显示,域对象值可传递)//request.getRequestDispatcher("success.jsp").forward(request,response);//跳转到success.jsp页面request.getRequestDispatcher("success").forward(request,response);//跳转到sucess Servlet页面//重定向//重定向(两次url地址,地址改变,域对象值无法传递)(拿到的值为null)//response.sendRedirect(request.getContextPath()+"/success");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}
}
SuccessServlet
@WebServlet(name = "success", value = "/success")
public class SuccessServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置请求响应编码格式为utf-8request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=UTF-8");//获取LoginServlet 传递下来的数据String user = (String)request.getAttribute("user");String ip = (String)request.getAttribute("ip");response.getWriter().append("用户名:" + user).append("<br>").append("ip:" + ip);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request,response);}
}