在jsp页面实现保存登录用户名和密码

loginindex.jsp:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<%@ page language="java" import="java.util.*,java.net.* " pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
try{
    Cookie[] cookies = request.getCookies();
    if(cookies != null){
        for(int i = 0; i < cookies.length; i++){
            if(cookies[i].getName().equals("username")){
                String username=URLDecoder.decode(cookies[i].getValue(), "utf-8");
                request.setAttribute("username",username);
            }
            if(cookies[i].getName().equals("password")){
                String password= URLDecoder.decode(cookies[i].getValue(), "utf-8");
                request.setAttribute("password",password);
            }
        }
    }
}catch(Exception e){
    e.printStackTrace();
}
%>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'loginindex.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="css/info/loginindex.css">
<script type='text/javascript' src='js/jquery-latest.js'></script>
<script type="text/javascript">
function check(){
    var murl="<%=request.getContextPath()%>/LoginIndexServlet";
    var username=encodeURI(document.getElementById("username").value);
    var password=document.getElementById("password").value;
    var a1="";
    var a2="";
    if(document.getElementById("a1").checked){
        a1="uname";
    }
    if(document.getElementById("a2").checked){
        a2="upass";
    }

    $.post(murl,{username:username,password:password,a1:a1,a2:a2} ,function(data){
        var json=eval("(" + data + ")");
        if(json.result=="success"){
            window.location.href="<%=request.getContextPath()%>/info/myinfo.jsp?username="+username;
        }else{
            window.location.href="<%=request.getContextPath()%>/info/loginindex.jsp";
        }   
    });
}

</script>

  </head>
 
  <body>
    <div class="head1">
    <input class="loc1" type="button" value="返回" οnclick="javascript :history.back(-1);">
    <font class="loc2">登录注册</font>
    </div>
    <div class="logo">
    <img src="images/headerLogo.png"></img>
    </div>
    <div class="form1">
    <form>
    <table align=center>
    <tr><td>用户名:</td><td><input type="text" value="${username}" name="username" id="username"/></td><tr>
    <tr><td>密 码:</td><td><input type="password" value="${password}" name="password" id="password"/></td><tr>
    </table><br/>
    <input type=checkbox id='a1' value="a1">保存登录名</input>
    <input type=checkbox id='a2' value="a2">保存密码</input><br/><br/><br/> 
    <input type="button" οnclick="window.location='<%=request.getContextPath()%>/info/register.jsp'" value="快速注册">
    <input type="button" value="登  录" οnclick="check() ">
    </form>
    </div>
  </body>
</html>


LoginIndexServlet:

package info.test.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLEncoder;

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 LoginIndexServlet 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 {
       
        response.setContentType("text/html;charset=utf-8");
        String msource="http://mobile.51bi.com/login.do";
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String a1=request.getParameter("a1");
        String a2=request.getParameter("a2");
        if("uname".equals(a1)){
            System.out.println("a1不为空");
            try{
                Cookie cookie1 = new Cookie("username", username);
                cookie1.setMaxAge(30 * 60 * 24 * 30 * 2); //用户名保留1个月
                response.addCookie(cookie1);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }else{
            System.out.println("a1为空");
        }
        if("upass".equals(a2)){
            System.out.println("a2不为空");
            try{
                Cookie cookie2 = new Cookie("password", URLEncoder.encode(password,"utf-8"));
                cookie2.setMaxAge(30 * 60 * 24 * 30 * 2); //用户名保留1个月
                response.addCookie(cookie2);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }else{
            System.out.println("a2为空");
        }

        msource=msource+"?username="+username+"&password="+password;
        System.out.println(msource);
        URL url = new URL(msource); 
        InputStream is = url.openStream(); 
        byte[] b = new byte[is.available()]; 
        is.read(b);       
        String resultString=new String(b, "utf-8");
        String tempString=new String(resultString.getBytes("iso-8859-1"),"utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        System.out.println(resultString);
        out.print(resultString);
        is.close();       
    }

    /**
     * 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 {

        this.doGet(request, response);
    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值