login.jsp
<formname= "F1"Method= "Get"Action= "Servlet/loginservlet"> <Table> <TR> <TD>Username:</TD> <TD><inputtype= "text"name= "username"value= "${un}"></TD> </TR> <TR> <TD>Password:</TD> <TD><inputtype= "Password"name= "Password"value="${PW}"></TD> </TR> <TR> <TD>Autologin:</TD> <TD><inputtype= "checkbox"name= "Auto"value= "1"></TD> </TR> <TR> <TDcolspan= "2"Align= "Center"><inputtype= "Submit"value= "Login"> </TD> </TR> </Table> </form>
Preparelogin
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {cookie[] cs = request.getcookies (); if (cs!=null && cs.length>0) {for (int i = 0; I<CS. length; i++) {Cookie C= Cs[i]; System.out.println (cs.length); System.out.println (i+ "--" +cs[i].getname ()); if (C.getname (). Equals ("username")) {String value= C.getvalue (); request.setattribute ("un", value); System.out.println (value); } if (C.getname (). Equals ("password")) {String value= C.getvalue ()
Save cookies
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { TODO auto-generated Method Stub String Auto = Request.getparameter ("Auto"); String username = request.getparameter ("username"); String Password = request.getparameter ("password"); if (auto! = null && auto.equals ("1")) {cookie u = new Cookie ("username", username); Cookie p = new Cookie ("password", password); U.setmaxage (60*60); P.setmaxage (60*60); Response.addcookie (U); Response.addcookie (P); } PrintWriter w = Response.getwriter (); W.println ("<HTML>"+" Hello "+"</HTML>" ); W.flush (); W.close (); }
Use cookies to remember user names and passwords