The servlet method uses cookies to remember the username and password for Logon. The servletcookie

Source: Internet
Author: User

The servlet method uses cookies to remember the username and password for Logon. The servletcookie

1. Establish a web Project

2. Create a servlet package

 

3. Right-click the package and create a new servlet. The path removes the previous servlet. You only need the doPost and doGet methods.

Compile servlet

The CookieServlet. java code is as follows:

Package test1029; 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 CookieServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this. doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html"); String uname = request. getParameter ("uname"); String password = request. getParameter ("password"); String ck = request. getParameter ("ck"); // The selected state is: on. if not selected, it is null if ("on ". equals (ck) {// construct the Cookie object // Add to the Cookie c = new Cookie ("users", uname + "-" + password ); // set the expiration time c. setMaxAge (600); // store response. addCookie (c );}}}

 

Configuration File web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>test1029</display-name>  <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>CookieServlet</servlet-name>    <servlet-class>test1029.CookieServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>CookieServlet</servlet-name>    <url-pattern>/CookieServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

Index. jsp code:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <% // el expression String names = ""; String pwd = ""; // retrieve CookieCookie [] c = request. getCookies (); for (int I = 0; I <c. length; I ++) {if (c [I]. getName (). equals ("users") {// stores data (username + password) names = c [I]. getValue (). s Plit ("-") [0]; pwd = c [I]. getValue (). split ("-") [1]; // save it again (standby) request. setAttribute ("xingming", names); request. setAttribute ("mima", pwd) ;}%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

:

1. After the project runs, enter the logon interface and enter the user name and password. The result is as follows:

 

2. After Logon:

 

 

3. Access the logon page again. The effect is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.