感觉对Servlet的最基本的东东还不是特别了解,下面简单复习一下。
第一个jsp:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>index.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<form action="Login" method="post">
name:<input type="text" name="userName">
<br>
password:<input type="password" name="userPassword">
<br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
一个Servlet:
package com.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
public class Login extends HttpServlet {
private static final long serialVersionUID = 3388966044136481743L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
response.setHeader("cache-control", "no-cache");
response.setHeader("pragma","no-cache");
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
if(null == userName || "".equals(userName = userName.trim()))
{
out.println("UserName can't be null!");
return;
}
if(null == userPassword)
{
out.println("UserPassword can't be null!");
return;
}
System.out.println("userName : " + userName + "\tpassword : " + userPassword);
request.setAttribute("userName", userName);
HttpSession session = request.getSession();
session.setMaxInactiveInterval(120);
session.setAttribute("userName", userName);
Cookie cookie = new Cookie("userName",userName);//Cookie是new出来的
cookie.setMaxAge(120);
response.addCookie(cookie);//要记得把它加到response中
ServletContext application = this.getServletContext();//application是这样出来的
application.setAttribute("userName", userName);
request.getRequestDispatcher("res.jsp").forward(request, response);
//如果上面的一行改为分别用下面这两个,会是什么效果呢?
//response.sendRedirect("res.jsp");
//application.getRequestDispatcher("/res.jsp").forward(request, response);
}
}
配置Servlet:
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
第二个jsp:res.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>res.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
</head>
<%
String userName = (String)request.getAttribute("userName");
String userName2 = (String)session.getAttribute("userName");
String userName3 = null;
String userName4 = (String)application.getAttribute("userName");
Cookie[] cookies = request.getCookies();
if(null != cookies && cookies.length > 0)
{
for(Cookie cookie : cookies)
{
//System.out.println("cookie name : " + cookie.getName() + "\tcookie value : " + cookie.getValue());
if("userName".equals(cookie.getName()))
userName3 = cookie.getValue();
}
}
%>
<body>
<%=path%>
<br>
<%=basePath%>
<br>
注意一下上面的东东<br>
测试的时候可以在浏览器中另开一个关于这个jsp的tab
<hr>
I'm from request.getAttribute : <%=userName%>
<br>
I'm from session.getAttribute : <%=userName2%>
再弄一个页面,在session不超时的情况下,也是能取到的。
<br>
I'm from cookie.getValue : <%=userName3%>
再弄一个页面,在cookie不超时的情况下,即使在这个时间段内重启了服务器,也是能取到的。
<br>
I'm from application.getAttribute : <%=userName4%>
在启动一个新的其他浏览器,也是能访问到的。
</body>
</html>