Java 如何从一个 servlet 文件调用另一个 servlet 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20947806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:29:51  来源:igfitidea点击:

How can i call from one servlet file to another servlet file?

javajspservletsnullpointerexception

提问by selvam

I am using net beans 7.1 and i create one JSP file with two servlet files. like

我使用的是 net beans 7.1,我创建了一个带有两个 servlet 文件的 JSP 文件。喜欢

index.jsp --->servlet1.java --->servlet2.java

index.jsp --->servlet1.java --->servlet2.java

I give some value from index.jspfile and send to servlet1.java.

我从index.jsp文件中给出一些价值并发送到servlet1.java.

In this servlet1.javafile I call servlet2.javafile.

在这个servlet1.java文件中,我将servlet2.javafile.

Then it throws NullPointerException.
How can I solve this?

然后它抛出NullPointerException
我该如何解决这个问题?

My code like this.

我的代码是这样的。

index.jsp

索引.jsp

<form  action="servlet1" method="post">  

servlet1.java

servlet1.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

                              ..................
                              ..................
                              ..................
        servlet2 ob=new servlet2();
        ob.doPost(request, response);
                              ..................
                              ..................
                              ..................
       }

Then it throws NullPointerException.

然后它抛出NullPointerException

采纳答案by Aniket Kulkarni

Use RequestDispatcher

使用RequestDispatcher

RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.forward(request,response);

RequestDispatcher

请求调度器

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.

定义一个对象,该对象接收来自客户端的请求并将它们发送到服务器上的任何资源(例如 servlet、HTML 文件或 JSP 文件)。



Update

更新

No need to create an object of servlet manually, just simply use RequestDispatcherto call servlet because web container controls the lifecycleof servlet.

不需要手动创建servlet对象,直接RequestDispatcher调用servlet即可,因为web容器控制 servlet的生命周期

From Oracle JavaEE docs Servlet Lifecycle

来自 Oracle JavaEE 文档Servlet 生命周期

The lifecycle of a servlet is controlled by the container in which the servlet has been deployed.
When a request is mapped to a servlet, the container performs the following steps.

servlet 的生命周期由部署 servlet 的容器控制。
当请求映射到 servlet 时,容器执行以下步骤。

  1. If an instance of the servlet does not exist, the web container

    • Loads the servlet class.

    • Creates an instance of the servlet class.

    • Initializes the servlet instance by calling the init method. Initialization is covered in Creating and Initializing a Servlet.

  2. Invokes the service method, passing request and response objects. Service methods are discussed in Writing Service Methods.

  1. 如果 servlet 的实例不存在,则 Web 容器

  2. 调用服务方法,传递请求和响应对象。服务方法在编写服务方法中讨论。

回答by Santhosh

What are you trying here,

你在这里尝试什么,

servlet2 ob=new servlet2();
ob.doPost(request, response);

Its not necessary to create an object explicitly for a servlet, Web container creates an instance for a servlet and shares it during the app's lifetime. Though you have created an object here, it will return the existing object only.

不必为 servlet 显式创建对象,Web 容器会为 servlet 创建实例并在应用程序的生命周期内共享它。尽管您在此处创建了一个对象,但它只会返回现有对象。

You can is instead go for Request Dispatcher or page Redirect.

您可以改为使用Request Dispatcher 或 page Redirect