首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Cookie兑现自动登录

2012-12-23 
Cookie实现自动登录登录页面:???登录成功页面:??Servlet:?package com.TestCookie.bizimport java.io.IOE

Cookie实现自动登录

登录页面:

?

?

?登录成功页面:

?

?

Servlet:

?

package com.TestCookie.biz;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 javax.servlet.http.HttpSession;public class TestServlet extends HttpServlet {/** * 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 {this.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 {response.setContentType("text/html");PrintWriter out = response.getWriter();response.setCharacterEncoding("UTF-8");HttpSession session=request.getSession();String name=request.getParameter("name");String password=request.getParameter("password");if ("Camey".equals(name)&&"123456".equals(password)) {Cookie cookie=new Cookie("name", name);cookie.setMaxAge(10000);response.addCookie(cookie);session.setAttribute("name", name);response.sendRedirect(response.encodeRedirectURL("index.jsp"));}out.flush();out.close();}}
?

?

热点排行