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="data: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:

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);
}
}

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

  1. 在创建jsp页面之后保存时提示错误,并提示以UTF-8格式进行保存。

    在创建jsp页面之后保存时提示错误,并提示以UTF-8格式进行保存. Save could not be completed. Reason: some characters cannot be ma ...

  2. jsp使用cookie实现记住用户名和密码

    首先说一下实现的功能: 用户打开注册页面,最下面有个记住用户名和密码的复选框,如果勾选上,则在登录页面会自动将用户名和密码赋值到文本框中,使用java中的cookie实现,下面就是代码: 注册页面代码 ...

  3. 计算机怎么登录用户名和密码忘了怎么办,忘记了路由器的登录用户名与密码怎么办...

    由于一般路由器设置了一次后期很少需要再去设置,因此后期会出现当需要进入路由器的时候,自己也将登录用户名与密码忘记了,那么这种情况怎么办呢?主要分为2种情况: (电脑入门到精通网 www.58116.c ...

  4. 华三交换机如何进入配置_H3C交换机如何配置登录用户名和密码

    在中国交换机市场,H3C自2003年公司成立以来,已累计出货上百万台,以太网交换机端口数占全球的`市场份额23.4%,在交换机领域内综合技术实力和市场份额排名均达到业界第一.下面YJBYS小编为大家整 ...

  5. 电脑开始怎么设置计算机用户名,如何修改电脑登录用户名以及密码呢?5秒钟让你学会...

    如何修改电脑登录用户名以及密码呢?对于这个问题,你可能有点好笑,这么简单的问题还拿出来分享呢? 确实,对于稍微懂一点电脑知识的人而言,确实超级简单可笑,但我也相信还是有部分初学者以及对于电脑知识不是太 ...

  6. 删除Windows下samba保存的用户名和密码

    当我们访问Windows共享目录或者NAS共享盘时,Windows会提示输入共享用户名和密码,这时如果我们选择了"记住我的凭据",Windows就会将认证凭证保存到计算机中,这样下 ...

  7. H3C Telnet、SSH登录 用户名和密码一致

    H3C Telnet.SSH登录 一.项目拓扑 二.项目需求 三.配置步骤 四.测试 一.项目拓扑 二.项目需求 telnet可登录 ssh可登录 用户名和密码一致都是abc, 用户权限15 三.配置 ...

  8. python ftplib模块 爆破ftp登录用户名和密码

    1.连接ftp服务器 import ftplib ftp = ftplib.FTP() ftp.connect('192.168.50.130', 21) ftp.login("anonym ...

  9. vue 解决密码框 input 中type=“password“时,浏览器会回显之前保存的用户名和密码

    input的type="password"时,会出现图中现象,之前保存的用户名和密码回显出来 1.为了解决这个问题尝试过以下几种方法: autocomplete="off ...

最新文章

  1. 证明连通简单图是哈密顿图
  2. 用DL深度学习神经网络绘图---对于程序来说0和1到底是什么样的
  3. 2009年全国计算机软件考试推荐用书目录
  4. phplivechat安卓app下载_掌音教师端下载软件-掌音教师版app下载v1.5.1 安卓官方版...
  5. (十)python3 只需3小时带你轻松入门——模块与包
  6. DPMM的理解、公式推导及抽样
  7. JNI中访问JList的代码
  8. 蒙特卡洛模型之神奇的布丰投针实验
  9. C语言:文件的读写操作
  10. Word | 图片被文字遮挡
  11. 计算机记账的优点,记账凭证的优缺点
  12. 使用 Microsoft RDC for Mac 在 Mac 和 Windows 间传文件
  13. 用树莓派学Linux(三)--------SSH端口转发,跳出局域网的树莓派控制
  14. 2020TI省级大学生电子竞赛推荐芯片简介
  15. 【前端三分钟】利用Javascript实现打字效果
  16. 【强烈推荐】机器学习之算法篇
  17. 【Android - 技术期刊】第002期
  18. MFC中使用CPropertySheet实现Tab Control
  19. 苹果全系产品信息查询
  20. idea2018 2020_新生2020级数字艺术系学生作品展

热门文章

  1. 视频API的发展方向
  2. 北京大学数字视频编解码技术国家工程实验室开源AVS2高清实时编码器
  3. 下篇 | 说说无锁(Lock-Free)编程那些事(下)
  4. 如何让xtrabackup恢复速度提升20倍?
  5. 腾讯AI Lab刘霁谈机器学习,异步计算和去中心化是两大杀器
  6. 熊本熊之父来鹅厂啦!这是一场关于设计和体验的有趣之旅!
  7. 练习:每一分钟产生一个文件,保存本分钟内所有数字之和。
  8. 【SpringBoot实战】员工部门管理页面,增删改查,含源码
  9. Libra教程之:来了,你最爱的Move语言
  10. 深入理解Memory Order