Servlet状态追踪

1:问题的提出:如何让只有登录的用户才能访问某些网页以及如何判断登录。
2:解决上述问题的方案有:
   1)通过Cookie方式来实现
   2)通过Session方式来实现

  Cookie实现方式:
    
    Cookie定义:  Cookie是一小块可以嵌入HTTP请求和响应的数据。它在服务器上产生,并作为响应头域的一部分返回给客户。浏览器在收到有Cookie的响应后,会把Cookie的内容以"关键字/值"的形式写入到一个专为存放Cookie的文本文件中。浏览器会把Cookie的信息与请求发送给服务器,这样服务器就可以再次读取Cookie中存放的数据了。Cookie可以对有效期进行设置,浏览器不会把过期的Cookie发送给服务器。
  
  步骤:1)创建一个Cookie对象;
        2)用户登录之后自动将其该Cookie对象加到服务器端去
        3)用户再次访问的时候,服务器判断该该特定的Cookie存不存在,如果不存在,则要求用户重新登录,如果存在,则可以跳过登录的界面。
  登陆界面
package testParameter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TableP extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=gb2312");
		PrintWriter out=response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<form action='/JSP/tabletest' method='post'>");
		out.println("<table align='center'>");
		
		out.println("<tr>");
		out.println("<td>");
		out.println("username:");
		out.println("</td>");
		out.println("<td>");
		out.println("<input type='text' name='name'>");
		out.println("</td>");
		out.println("</tr>");
		
		out.println("<tr>");
		out.println("<td>");
		out.println("password:");
		out.println("</td>");
		out.println("<td>");
		out.println("<input type='password' name='pwd'>");
		out.println("</td>");
		out.println("</tr>");
		
		out.println("<tr >");
		out.println("<td colspan='2'>");
		out.println("<input type='submit' value='提交'>");
		out.println("</td>");
		out.println("</tr>");
		
		out.println("</table>");
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");	
		out.flush();
	}
}

验证界面:
package testParameter;

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

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	
		String username="aaa";
		String userpwd="bbb";
		
		String name=req.getParameter("name");
		String pwd=req.getParameter("pwd");
		System.out.println(name);
		System.out.println(pwd);
	    if(username.equals(name)&&userpwd.equals(pwd)){
	    	Cookie cookie=new Cookie("testCookie","userid");
	    	cookie.setMaxAge(15);
	    	resp.addCookie(cookie);
	    	req.getRequestDispatcher("/data").forward(req, resp);
	    }else{
	    	req.getRequestDispatcher("/error").forward(req, resp);
	    }
	}
	
}


cookie存在,则可以直接登录界面
package testParameter;

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

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		Boolean bool = false; // 判断是否有Cookie存在

		Cookie[] cookies = request.getCookies();

		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("testCookie")){
					bool = true;
					break;
				}
			}
		} else {
System.out.println("==========================error!");
			request.setAttribute("error", "information");
			request.getRequestDispatcher("/error").forward(request, response);
			return;
		}

		if (bool) {
			out.println("<html>");
			out.println("<body>");
			out.println("cookie 成功");
			out.println("</body>");
			out.println("</html>");
		}
		out.flush();
	}

}



你可能感兴趣的:(java,html,浏览器,servlet)