index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> </head> <form action="./servlet/LoginServlet" method="post"> <body> 员工信息录入<br/> 用户名:<input type="text" value="" name="username" /> <br/> 密码:<input type="password" name="psw"/> <br/> 保存用户名和密码<input type="checkbox" name="save" value="yes" /><br/> 性别:<input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女"/>女 <br/> 学历:<select name="edu"> <option value="硕士研究生">硕士研究生</option> <option value="博士研究生">博士研究生</option> <option value="本科生">本科生</option> </select><br/> 爱好:<br/><input type="checkbox" name="love" value="睡觉">睡觉<br/> <input type="checkbox" name="love" value="学习">学习<br/> <input type="checkbox" name="love" value="交友">交友<br/> <input type="checkbox" name="love" value="旅游">旅游<br/> 全选/全不选 <input type="checkbox" name="checkall" value="yes"><br/> 描述:<textarea rows="10" cols="10" name="des"></textarea> 多框:<select name="items" multiple="multiple" size="6"> <option value="选项1">选项1</option> <option value="选项2">选项2</option> <option value="选项3">选项3</option> <option value="选项4">选项4</option> <option value="选项5">选项5</option> <option value="选项6">选项6</option> </select><br/> <input type="submit" value="保存" /> </body> </form> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>day09.LoginServlet</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>GetCGIServlet</servlet-name> <servlet-class>day09.GetCGIServlet</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ReadCookieServlet</servlet-name> <servlet-class>day09.ReadCookieServlet</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LastDateServlet</servlet-name> <servlet-class>day09.LastDateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlet/LoginServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetCGIServlet</servlet-name> <url-pattern>/servlet/GetCGIServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ReadCookieServlet</servlet-name> <url-pattern>/servlet/ReadCookieServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LastDateServlet</servlet-name> <url-pattern>/LastDateServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
package day09; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.util.Set; 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 LoginServlet extends HttpServlet { /** * Constructor of the object. */ public LoginServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ /* * request 封装了客户端的所有请求信息 * response 封装了客户端的所有响应信息 */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //创建cookie Cookie usernameCookie=new Cookie("username","admin"); //服务器在响应头中设置cookie response.addCookie(usernameCookie); /* * 设置cookie为持久Cookie * 7天;setMaxAge()以秒为单位 */ usernameCookie.setMaxAge(7*24*3600); /* * 设置路径,使cookie在整个路径都好用 */ String contextPath=request.getContextPath(); usernameCookie.setPath(contextPath); System.out.println("request:"+request); //获取请求方法 String method=request.getMethod(); System.out.println("method:"+method); String username=request.getParameter("username"); String psw=request.getParameter("psw"); String usernameValue=""; String pswValue=""; Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(int i=0;i<cookies.length;i++){ Cookie cookie=cookies[i]; //System.out.println("cookie的名称:"+cookie.getName()); if("username".equals(cookie.getName())){ usernameValue=cookie.getValue(); } if("psw".equals(cookie.getName())){ pswValue=cookie.getValue(); } } } @SuppressWarnings("unused") Cookie usernameCookie2=null; if(username!=null&&!"".equals(username.trim())){ usernameCookie2=new Cookie("username",username.trim()); } @SuppressWarnings("unused") Cookie pswCookie2=null; if(psw!=null&&!"".equals(psw.trim())){ pswCookie2=new Cookie("psw",psw.trim()); } String saveCookie=request.getParameter("save"); if(saveCookie!=null){ System.out.println("saveCookie被选中"); if(usernameCookie2!=null){ usernameCookie2.setMaxAge(7*24*3600); } if(pswCookie2!=null){ pswCookie2.setMaxAge(7*24*3600); } }else{ System.out.println("saveCookie没被选中"); if(usernameCookie2!=null){ usernameCookie2.setMaxAge(0); } if(pswCookie2!=null){ pswCookie2.setMaxAge(0); } } //加入Cookie到应答头 if(usernameCookie2!=null){ response.addCookie(usernameCookie2); } if(pswCookie2!=null){ response.addCookie(pswCookie2); } String sex=request.getParameter("sex"); String edu=request.getParameter("edu"); String[] items=request.getParameterValues("items"); String[] love=request.getParameterValues("love"); String checkall=request.getParameter("checkall"); if(checkall==null){ System.out.println("复选框没有被全选中!"); } String des=request.getParameter("des"); //////******************************************** /* * radio按钮,checkbox按钮,多选,下拉选,若没被选中, * 则在服务器端获取不到。 */ Enumeration<String> em=request.getParameterNames(); while(em.hasMoreElements()){ String name=em.nextElement(); String[] value=request.getParameterValues(name); if(value!=null&&value.length>0){ for(int i=0;i<value.length;i++) out.println(name+"~~~"+value[i]+"<br/>"); } } /*********** * Map<String,String[]> * 第一个String:表示form表单中的name * 第二个String[]:表示value */ Map<String,String[]> map=request.getParameterMap(); Set set=map.keySet(); Iterator<String> it=set.iterator(); while(it.hasNext()){ String key=it.next(); String[] nameValue=map.get(key); if(nameValue!=null&&nameValue.length>0){ for(int i=0;i<nameValue.length;i++) out.println(key+"~~~"+nameValue[i]+"<br/>"); } } /*********** * 获取请求头信息 */ Enumeration<String> header=request.getHeaderNames(); while(header.hasMoreElements()){ String headerName=header.nextElement(); String headerValue=request.getHeader(headerName); out.println(headerName+"~~~"+headerValue); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
package day09; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GetCGIServlet extends HttpServlet { /** * Constructor of the object. */ public GetCGIServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //从响应对象中获取字符输出流 PrintWriter out = response.getWriter(); //从响应对象中获取字符输出流(下载,处理图片等二进制的信息) //OutputStream outstream=response.getOutputStream(); //获取cgi变量 //获取web应用上下文********* String contextPath=request.getContextPath(); out.println("contextPath= "+contextPath); //获取远程IP*********** String remoteIP=request.getRemoteAddr(); out.println("remoteIP= "+remoteIP); //获取查询字符串 String queryString=request.getQueryString(); out.println("queryString= "+queryString); //获取servlet名称************ String servletPath=request.getServletPath(); out.println("servletPath= "+servletPath); //获取url String url=request.getRequestURI(); out.println("url= "+url); //获取uri String uri=request.getRequestURI(); out.println("uri= "+uri); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
package day09; import java.io.IOException; import java.io.PrintWriter; 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 ReadCookieServlet extends HttpServlet { /** * Constructor of the object. */ public ReadCookieServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //获取浏览器传递过来的cookie Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(int i=0;i<cookies.length;i++){ Cookie cookie=cookies[i]; System.out.println("cookie的名称:"+cookie.getName()); if("username".equals(cookie.getName())){ out.println("客户端传过来的Cookie= "+cookie.getValue()); } } } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
package day09; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import day09util.DataUtil; public class LastDateServlet extends HttpServlet { /** * Constructor of the object. */ public LastDateServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); Cookie lastData=new Cookie("lastdate",DataUtil.getDateStr()); //设置cookie保存时间,加入应答头 lastData.setMaxAge(365*24*60*60); response.addCookie(lastData); //获取客户端传递过来的cookie Cookie[] cookies=request.getCookies(); if(cookies!=null&&cookies.length>0){ for(int i=0;i<cookies.length;i++){ Cookie cookie=cookies[i]; System.out.println("cookie的名称:"+cookie.getName()); if("lastdate".equals(cookie.getName())){ out.println("客户端传过来的Cookie= "+cookie.getValue()); } } } } }
package day09util; import java.text.SimpleDateFormat; import java.util.Date; public class DataUtil { /* * 获取日期格式 */ public static String getDateStr(){ String str=null; SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); str=sf.format(new Date()); return str; } public static void main(String[] args) { System.out.println(getDateStr()); } }