// 设置服务器的编码格式和浏览器查看要使用的编码格式
response.setContentType("text/html;charset=UTF-8")
request.setCharacterEncoding("UTF-8")
// 获取表单中所有提交的数据打印
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")
System.out.println(username)
System.out.println(password)
System.out.println(sex)
if (hobbys != null) {
for (String string : hobbys) {
System.out.println(string)
}
}
if (!city.equals("---请选择---")) {
System.out.println(city)
}
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String[] values = request.getParameterValues(name);
for (String string : values) {
System.out.println(string);
}
}
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
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();
}
请求转发,请求重定向与请求包含
@SuppressWarnings("serial")
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");
PrintWriter out = response.getWriter();
request.setAttribute("name", "six");
request.getRequestDispatcher("/demo02").include(request, response);
out.write("123");
System.out.println("demo01 结尾");
}
public void fun1(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/Demo/demo01");
}
/**
*
*/
public void fun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/demo01");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
@SuppressWarnings("serial")
public class Demo02 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("Demo02" + name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
会话技术
@SuppressWarnings("serial")
public class Demo 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("/Demo/servlet");
cookie.setMaxAge(60 * 5);
response.addCookie(cookie);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
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");
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);
}
}
public class Demo 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 = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
}
}
}
response.getWriter().write("<form action=\"/Demo/demo01\" method=\"post\">");
if (username != null) {
response.getWriter().write("用户名:<input type=\"text\" name=\"username\" value=username>");
} else {
response.getWriter().write("用户名:<input type=\"text\" name=\"username\">");
}
response.getWriter().write("<br>");
response.getWriter().write("密码:<input type=\"password\" name=\"password\">");
response.getWriter().write("<br>");
response.getWriter().write("记住我:<input type=\"checkbox\" name=\"remember\">");
response.getWriter().write("<br>");
response.getWriter().write("<input type=\"submit\" name=\"submit\" value=\"提交\">");
response.getWriter().write("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
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 = request.getParameter("username");
String password = request.getParameter("password");
String remember = request.getParameter("remember");
Cookie cookie = new Cookie("username", username);
cookie.setPath("/");
if (username.equals("test") && password.equals("123")) {
if (remember != null) {
System.out.println("设置存活时间");
cookie.setMaxAge(Integer.MAX_VALUE);
} else {
cookie.setMaxAge(0);
}
response.addCookie(cookie);
response.getWriter().write("登录成功");
} else {
response.getWriter().write("登录失败");
response.setHeader("refresh", "3;url=/Demo/demo");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
http://blog.csdn.net/huzongnan/article/list