request获取form表单中提交的数据
用ServletContext获取
ServletContext context = this.getServletContext();
String username = context .getInitParameter("username" );
用request获取
用request 获取表单中的信息时自己不用创建对象,request 比ServletContext更常用
1.html
<html >
<head >
<meta charset ="UTF-8" >
<title > Insert title heretitle >
head >
<body >
<form action ="/sh-web-servlet06/demo01" method ="get" >
用户名:<input type ="text" name ="username" /> <br />
密码:<input type ="text" name ="password" /> <br />
男<input type ="radio" name ="sex" value ="男" checked ="checked" > <br />
女<input type ="radio" name ="sex" value ="女" > <br />
<input type ="checkbox" name ="hobby" value ="篮球" > 篮球<br />
<input type ="checkbox" name ="hobby" value ="唱歌" > 唱歌<br />
<input type ="checkbox" name ="hobby" value ="画画" > 画画<br />
<select name ="city" >
<option > --- 请选择城市 ---option >
<option name ="bj" > 北京option >
<option name ="sh" > 上海option >
<option name ="tj" > 天津option >
select ><br />
<input type ="submit" value ="提交" >
form >
body >
html >
web.xml
<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" >
<display-name > sh-web-servlet06display-name >
<welcome-file-list >
<welcome-file > index.htmlwelcome-file >
<welcome-file > index.htmwelcome-file >
<welcome-file > index.jspwelcome-file >
<welcome-file > default.htmlwelcome-file >
<welcome-file > default.htmwelcome-file >
<welcome-file > default.jspwelcome-file >
welcome-file-list >
<servlet >
<servlet-name > demo01servlet-name >
<servlet-class > com.lanou3g.Demo01servlet-class >
servlet >
<servlet-mapping >
<servlet-name > demo01servlet-name >
<url-pattern > /demo01url-pattern >
servlet-mapping >
web-app >
Demo01
public class Demo01 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username" );
System.out.println(username);
String password = request.getParameter("password" );
System.out.println(password);
String sex = request.getParameter("sex" );
System.out.println(sex);
String[] hobbys = request.getParameterValues("hobby" );
for (String hobby : hobbys) {
System.out.println(hobby);
}
String city = request.getParameter("city" );
System.out.println(city);
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
结果截图
getParameterNames()方法
public void doGet(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
Enumeration<String > names = request .getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String [] values = request .getParameterValues(name);
for (String value : values) {
System.out.println(value);
}
}
}
结果截图
导包调用包中的方法
导入两个包commons-beanutils - 1.9 .3 . jar和commons-logging - 1.2 . jar,其中
commons-beanutils - 1.9 .3 . jar依赖 commons-logging - 1.2 . jar,调用commons-beanutils - 1.9 .3 . jar
包中的方法,来获取form表单中的数据
调用request方法getParameterMap()来获取map 集合
BeanUtils. populate(user, map );
把表单数据封装到对象中
参数1 要封装的对象 注意:该对象要符合javabean规范
参数2 map 集合
建立实体类users
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map < String , String [ ] > map = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user, map);
System.out.println(user);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
结果截图
请求转发 重定向 请求包含
请求包含:只能站内访问 相当于把两个页面的响应合成一个响应 返回给浏览器
请求转发:只能站内访问 一次请求 只是服务器内部的访问 不管你怎么写路径 始终都在项目路径下
重定向:站内站外都可以访问 重定向既可以访问本地服务器 也可以访问非本地服务器 两次请求
如果在AServlet中请求转发到BServlet,那么在AServlet中不允许再输出响应体,即不能使用response.getWriter () 和
response.getOutputStream () 向客户端输出,这一工作交由BServlet来完成;如果是由AServlet请求包含BServlet,则没有这个限制
请求转发
private void fun1(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
request .getRequestDispatcher("/demo03" ).forward(request , response );
PrintWriter writer = response .getWriter();
writer.write("我是demo02" );
System.out.println("我是demo02..." );
}
public void doGet(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
response .setContentType("text/html;charset=UTF-8" );
request .setCharacterEncoding("UTF-8" );
PrintWriter writer = response .getWriter();
writer.write("我是demo03" );
System.out.println("我是demo03..." );
}
public void doPost(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
doGet(request , response );
}
重定向
private void fun2 (HttpServletResponse response) throws IOException {
response.sendRedirect("/sh-web-servlet06/demo03" );
PrintWriter writer = response.getWriter();
writer.write("我是demo02" );
System.out .println("我是demo02..." );
}
请求包含
public void doGet(HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
response .setContentType("text/html;charset=UTF-8" );
request .setCharacterEncoding("UTF-8" );
request .getRequestDispatcher("/demo03" ).include(request , response );
PrintWriter writer = response .getWriter();
writer.write("我是demo02" );
System.out.println("我是demo02..." );
}
cookie
会话技术
客户端(浏览器)和服务端进行交互(会话)
当浏览器关闭的时候 这次交互(会话)结束
例如打电话
会话中的两个技术
1.cookie(客户端技术)
cookie是保存在浏览器中的缓冲数据
当我发起一个请求 请求一个servlet
然后进行逻辑处理(添加一个商品进购物车)
处理完成后给客户端(浏览器)一个响应
响应中携带着记录了购买了什么商品的cookie
让浏览器保存起来 可以保存在内存当中(结束会话 cookie被清除)
也可以保存在硬盘中(结束会话 依然存在 就是个文件)
当用浏览器再一次访问购物车的时候
会携带着之前保存的cookie
每个网站可以保存20个cookie 整个浏览器可以保存 300个
注意:第一次访问服务器的时候 是不会携带cookie去访问的
因为cookie还没有产生,只有当第一次请求后的响应中可以把cookie
写回到浏览器中
每次刷新页面时,输出的时间都会改变
public class Demo04 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8" );
request.setCharacterEncoding("UTF-8" );
Cookie[] cookies = request.getCookies();
if (cookies != null ) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("lastTime" )) {
String value = cookie.getValue();
long time = Long.parseLong(value);
Date date = new Date(time);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
String lastTime = dateFormat.format(date);
response.getWriter().write("上次的访问时间:" + lastTime);
}
}
}
Cookie cookie = new Cookie("lastTime" , System.currentTimeMillis() + "" );
cookie.setPath("/sh-web-servlet05/servlet" );
cookie.setMaxAge(60 * 5 );
response.addCookie(cookie);
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Demo05中获取Demo04中设置的时间时,该时间不会改变,因为demo05中只有时间的获取,没有时间的设置
public class Demo05 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null ) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("lastTime" )) {
String value = cookie.getValue();
long long1 = Long.parseLong(value);
Date date = new Date(long1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
String format = dateFormat.format(date);
response.getWriter().write(format);
}
}
}
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
demo06
package com.lanou3g;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo06 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8" );
request.setCharacterEncoding("UTF-8" );
String username = "" ;
Cookie[] cookies = request.getCookies();
if (cookies!=null ) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username" )) {
username=cookie.getValue();
}
}
}
System.out.println(username);
response.getWriter().write(" );
response.getWriter().write("用户名: " );
response.getWriter().write("密码: " );
response.getWriter().write("记住: " );
response.getWriter().write(" " );
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
demo07
package com.lanou3g;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Demo07 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8" );
request.setCharacterEncoding("UTF-8" );
String username = request.getParameter("username" );
String password = request.getParameter("password" );
String remember = request.getParameter("remember" );
Cookie cookie = new Cookie("username" , username);
cookie.setPath("/sh-web-servlet05/servlet" );
if (username.equals("wwwww" )&&password.equals("123" )) {
if (remember!=null ) {
cookie.setMaxAge(Integer.MAX_VALUE);
}else {
cookie.setMaxAge(0 );
}
response.addCookie(cookie);
response.getWriter().write("登录成功" );
}else {
response.getWriter().write("登录失败" );
response.setHeader("refresh" ,
"3;url=/sh-web-servlet05/servlet/demo06" );
}
}
public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}