方法一:
先使用getParameter()方法获取除hobby项之外每项的name值,hobby是多选,因此使用getParameterValues()返回一个字符串数组,因hobby也可不选,所以对爱好项进行判断,在爱好非空的基础下,遍历返回的值,最后依次打印各项的值。
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] hobbys = request.getParameterValues("hobby");
String city = request.getParameter("city");
if(hobbys != null) {
for(Sting h : hobbys) {
System.out.println(h);
}
}
System.out.println("username");
System.out.println("password");
System.out.println("sex");
System.out.println("city");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
方法二:
利用getParameterNames()方法返回的类似迭代器,进行对表单信息的打印
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> names = request.getParameterName();
if(names.hasMoreElements()) {
String name = names.nextElement();
String[] strings = name.getParameterValues(name);
for (String value : strings) {
System.out.println(value);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
方法三:
导入:commons-beanutils-1.9.3.jar
commons-logging-1.2.jar
运用这两个架包中populate()方法,把表单数据封装到对象中.
populate(bean, properties)
参数一:要封装的对象,注意该对象要符合Javabean规范
参数二:map集合
public class Demo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = new User();
Map<String, String[]> map = request.getParameterMap();
try {
BeanUtils.populate(user, map);
System.out.println(user);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
请求包含:
相当于把两个servlet的响应合成一个响应,返回给浏览器。
书写:
request.getRequestDispatcher("/demo02").include(request, response);
会话技术:
会话:客户端(浏览器)和服务端交互,直到浏览器关闭的这一过程叫一个会话。
例如:打电话。
会话中的两个技术:
客户端技术:
Cookie:保存在浏览器中的缓存数据。
每个网站可以保存20个Cookie,整个浏览器可以保存300个。
作用分析:浏览器对Servlet发起一个请求,Servlet在进行逻辑处理完后,响应一个带有客户操作的Cookie给浏览器保存,当客户进行相同操作时,会携带之前保留的Cookie去访问。
注意:第一次访问服务器时,是不会携带Cookie去访问的,只有当第一次请求后的响应中可以把Cookie写回到浏览器中。
Cookie存活时间:
负值表示浏览器关闭就消失
正值表示存活时间
0表示删除Cookie
获取浏览器上次访问时间:
public class Demo0 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-servlet03/servlet");
cookie.setMaxAge(60 * 5);
response.addCookie(cookie);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
记录登陆后用户名:
在浏览器中画登录界面,并且读取Cookie中缓存的记录。
public class Demo01 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("<form action='/sh-web-servlet03/servlet/demo07' method='post'>");
response.getWriter().write("用户名<input type='text' name='username' /><br />");
response.getWriter().write("密码<input type='password' name='password' /><br />");
response.getWriter().write("<input type='checkbox' name='remember' />记住<br />");
response.getWriter().write("<input type='submit' value='提交' />");
response.getWriter().write("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获取请求的参数,并且设置Cookie记录信息
public class Demo02 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-servlet03/servlet");
if (username.equals("zhangsan") && password.equals("123456")) {
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-servlet03/servlet/demo06");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}