标签:servlet
一:servlet获取内置对象
public void doGet(HttpServletRequestrequest, HttpServletResponse response)
throws ServletException,IOException {
doPost(request,response);
HttpSessionses=request.getSession();
System.out.println("sessionID="+ses.getId());
ses.setAttribute("username","Spring");
System.out.println("属性="+ses.getAttribute("username"));
ServletContextsc=super.getServletContext();
System.out.println("真是路径:"+sc.getRealPath("/"));
}
二:servlet跳转
客户端跳转:地址栏改变
public void doGet(HttpServletRequestrequest, HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
request.getSession().setAttribute("name","Spring");
request.setAttribute("info","春天来了");
response.sendRedirect("getInfo.jsp");
}
getInfo.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘getInfo.jsp‘ starting page</title>
</head>
<body>
<h3>session属性:<%=session.getAttribute("name") %></h3>
<h3>request属性:<%=request.getAttribute("info") %></h3>
</body>
</html>
结果:
session属性:Spring
request属性:null
服务器端跳转:地址栏不会改变
public void doGet(HttpServletRequestrequest, HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
request.getSession().setAttribute("name","Spring");
request.setAttribute("info","春天来了");
//实例化RequestDispatcher,同时设置跳转路径
RequestDispatcherrd=request.getRequestDispatcher("getInfo.jsp");
rd.forward(request,response);
}
结果:
session属性:Spring
request属性:春天来了
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:servlet
原文地址:http://blog.csdn.net/dzy21/article/details/47317091