zoukankan      html  css  js  c++  java
  • Servlet forward example

     

    Here's an example of how to forward from a servlet to a JSP in your J2EE code. I can never remember how to do a forward like this when I need it, so even though this example is pretty easy, I've put it out here so I can find it later.

    The typical scenario is that you're working on a Java servlet, and you need to forward the user from that servlet to a JSP. Assuming the name of the JSP is "searchResults.jsp", here's the code that will forward from your servlet to that JSP:

    String nextJSP = "/searchResults.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    dispatcher.forward(request,response);
    

      

    Note that this servlet code also assumes that you have the two objects request and response available from your servlet. These come with your doGet() and doPost() method signatures, so it's a pretty small assumption.

    That's all there is to it. Just make sure you don't forget that last line (dispatcher.forward()). I did that one time while teaching a Java class, and for a little while I couldn't figure out why we weren't seeing the JSP. No errors showing up anywhere, but the servlet just wasn't performing the forward to the JSP.

  • 相关阅读:
    web-火狐浏览器下载地址
    如何使用jmeter录制app脚本
    Jmeter参数化(jmter和badboy的配合使用)
    monkey常用命令详解(多参数化)
    monkey常用命令
    mysql数据库命令
    Linux命令
    循环语句的使用
    K8S学习笔记一:K8S的基础概念
    jmeter做尖峰测试(浪潮测试)
  • 原文地址:https://www.cnblogs.com/hephec/p/4586853.html
Copyright © 2011-2022 走看看