Servlet学习(1)

  • 取得HttpSession实例

    在Servlet中去个一个Session对象,可以通过HttpServletRequest接口完成。
    HttpSession ses = request.getSession();
  • 取得ServletContext实例

 

    Application内置对象是ServletContext借口的实例,表示是Servlet上下文

方法 类型   描述  
public ServletContext getServletContext 普通 取得ServletContext对象

    ServletContext application = super.getServletContext();

  • Servlet跳转

  • 客户端跳转

   在Servlet中如果想要进行客户端跳转,直接使用HttpServletResponse借口的sendRedirect()方法即可,但是需要注意的是,此跳转只能传递session 范围的属性,而无法传递request范围的属性。

<%=request.getAttribute("ss")%>值为空,<%=session.getAttribute("ss")%>能够取得到传进来的

由于是客户端跳转,所以跳转后的地址栏是会改变的。只能接收到session属性范围的内容,二request属性范围的内容是无法接收到的,这是因为request属性范围只有在服务器端跳转才有用。

response.sendRedirect("info.jsp")
  • 服务端跳转

    在服务器跳转中必须依靠RequestDispatcher借口完成。

  public void forward(ServletRequest request,ServletResponse response) throws ServletException,IOException 页面跳转

  public void include(ServletRequest request,ServletResponse response) throws ServletException,IOException 页面包含

  使用RequestDispatcher借口的frrward()方法即可完成跳转功能的实现,但是如果要想使用此借口还需要使用ServletRequest借口提供的

  public RequestDispatcher getRequestDispatcher(String path) 进行实例化

1 RequestDispatcher view=request.getRequestDispatcher("result.jsp");
2 view.forward(request, response);
View Code

  服务器端跳转后,页面的路径不会发生改变,而且此时可以在跳转后的JSP文件中接收到session以及request范围的属性  

原文地址:https://www.cnblogs.com/yixianyixian/p/3497521.html