AutoLogin 自动登录页面的制作

jsp代码:

login.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<head>


<title>login.jsp</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="styles.css">
	-->
<%
	String path = "/7ServletCookie";
%>
</head>

<body>
	<form action="<%=path%>/servlet/LoginServlet" method="post">
		<table border="0">
			<tr>
				<td>username:</td>
				<td><input type="textbox" name="username" value="${un }" />
				</td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" name="password" value="${pw }" />
				</td>
			</tr>
			<tr>
				<td>AutoLogin:</td>
				<td><input type="checkbox" name="auto" value="1" />
				</td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" name="auto0" value="Login" />
				</td>
			</tr>
		</table>
	</form>
</body>
LoginServlet:

package com.tao.cookie;

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 LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String auto = request.getParameter("auto");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if (auto != null && auto.equals("1")) {

			Cookie c1 = new Cookie("username", username);
			Cookie c2 = new Cookie("password", password);
			c1.setMaxAge(60 * 60);
			c2.setMaxAge(60 * 60);
			response.addCookie(c1);
			response.addCookie(c2);
		}else{
			Cookie killc1 = new Cookie("username", null);
			Cookie killc2 = new Cookie("password", null);
			killc1.setMaxAge(0);
			killc2.setMaxAge(0);
			response.addCookie(killc1);
			response.addCookie(killc2);
		}
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.println("welcome," + username);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}

PrepareServlet:
package com.tao.cookie;

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;

import sun.security.jgss.LoginConfigImpl;

public class PrepareServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public PrepareServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public 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];
				if (c.getName().equals("username")) {
					String value=c.getValue();
					request.setAttribute("un",value );
				}
				if (c.getName().equals("password")) {
					String value=c.getValue();
					request.setAttribute("pw",value );
				}
			}
		}
		request.getRequestDispatcher("/login.jsp").forward(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);

	}

	public void init() throws ServletException {
		// Put your code here
	}

}

1.PrepareServlet首次打开

2.PrepareServlet中填写表单

3.LoginServlet中保存cookie。

4.重新打开PrepareServlet页面,表单自动填写。


JSP代码中的<%=path %>不知道为什么会报错,只好加上
<%
String path = "/7ServletCookie";
%>
可能新建的时候用的模板不对。带form表单的都是框架的,还没学到。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值