日期:2014-05-18 浏览次数:20828 次
package kedun.dhy;
import javax.servlet.*;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class HttpSessionDemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
req.getSession().setAttribute("name", "测试");
req.setAttribute("info", "SZKEDUN");
resp.sendRedirect("get_info.jsp");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
this.doGet(req, resp);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>get_info.jsp</title>
</head>
<% request.setCharacterEncoding("UTF-8"); %>
<body>
<h2>名字:<%=session.getAttribute("name")%></h2>
<h2>属性:<%=request.getAttribute("info")%></h2>
<%
response.sendRedirect("HttpSessionDemoServlet.java");
%>
</body>
</html>