日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/1/23 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在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:

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页面实现保存登录用户名和密码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。