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-servlet-03display-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>
<servlet>
<servlet-name>demo02servlet-name>
<servlet-class>com.lanou3g.Demo02servlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo02servlet-name>
<url-pattern>/demo02url-pattern>
servlet-mapping>
<servlet>
<servlet-name>demo03servlet-name>
<servlet-class>com.lanou3g.Demo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo03servlet-name>
<url-pattern>/demo03url-pattern>
servlet-mapping>
<servlet>
<servlet-name>demo04servlet-name>
<servlet-class>com.lanou3g.Demo04servlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo04servlet-name>
<url-pattern>/servlet/demo04url-pattern>
servlet-mapping>
<servlet>
<servlet-name>demo05servlet-name>
<servlet-class>com.lanou3g.Demo05servlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo05servlet-name>
<url-pattern>/servlet/demo05url-pattern>
servlet-mapping>
<servlet>
<servlet-name>demo06servlet-name>
<servlet-class>com.lanou3g.Demo06servlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo06servlet-name>
<url-pattern>/servlet/demo06url-pattern>
servlet-mapping>
<servlet>
<servlet-name>demo07servlet-name>
<servlet-class>com.lanou3g.Demo07servlet-class>
servlet>
<servlet-mapping>
<servlet-name>demo07servlet-name>
<url-pattern>/servlet/demo07url-pattern>
servlet-mapping>
web-app>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<form action="/SH-web-servlet-03/demo01" method="post">
用户名:<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="篮球"/>篮球
<input type="checkbox" name="hobby" value="唱歌"/>唱歌
<input type="checkbox" name="hobby" value="画画"/>画画
<br>
<select name="city">
<option>---请选择---option>
<option>北京option>
<option>天津option>
<option>上海option>
select>
<br>
<input type="submit" value="提交">
form>
body>
html>
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");
User user = new User();
Map map = request.getParameterMap();
try {
BeanUtils.populate(user, map);
System.out.println(user);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
private void fun2(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement();
String[] values = request.getParameterValues(name);
for (String value : values) {
System.out.println(value);
}
}
}
private void fun1(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
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[] hobby = request.getParameterValues("hobby");
if (hobby != null) {
for (String h : hobby) {
System.out.println(h);
}
}
String city = request.getParameter("city");
System.out.println(city);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
请求转发 请求重定向 请求包含 获取Servlet域中的值
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");
request.setAttribute("name", "xiaoming");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("/demo03").include(request, response);
out.write("123");
System.out.println("demo02结尾");
}
private void fun2(HttpServletResponse response) throws IOException {
response.sendRedirect("/SH-web-servlet-03/demo03");
}
private void fun1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/demo03");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("456");
String name = (String)request.getAttribute("name");
System.out.println("Demo03" + name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
会话技术
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-servlet-03/servlet");
cookie.setMaxAge(5 * 60);
response.addCookie(cookie);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
cookie
public class Demo05 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);
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}