无法在 doPost 方法中获取请求属性



我正在尝试通过一个简单的java servlet更新我的数据库实体。在我的 UpdateFooServlet 的 doGet methosd 中,我使用了一个查询参数,它是我的 Foo 的 id,我通过我的数据库的 id 获取该 Foo 对象,并在我的请求中将其设置为"foo"属性。后来,在我的doPost方法中,我试图获取我的请求属性foo(为了写差异,但它是应用程序的另一部分不需要在这里(,一旦我尝试这样做,我就会得到NullPointerExeption。我也尝试将其设置为会话属性,就像我对 ,y 用户所做的那样,但它也不起作用。我哪里犯了错误?

@WebServlet("/UpdateFooServlet")
public class UpdateFooServlet extends HttpServlet {
    FooDAO fooDao = new FooDAO();
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Foo foo = fooDao.getFooById(id);
        System.out.println(foo.toString());
        request.setAttribute("foo", foo);
        RequestDispatcher dispatcher = request.getRequestDispatcher("updatefoo.jsp");
        dispatcher.include(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Foo notUpdatedFoo = (Foo) request.getAttribute("foo");
        System.out.println(notUpdatedFoo.toString());

我使用 System.out 来检查是否从 db 或请求属性中提取了完全相同的 Foo。

原木:

ERROR [io.undertow.request] (default task-7) UT005023: Exception handling request to /tasks-manager/UpdateFooServlet: java.lang.NullPointerException
    at servlets.UpdateFooServlet.doPost(UpdateFooServlet.java:38)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at filters.AuthenticationFilter.doFilter(AuthenticationFilter.java:38)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

请求对象的生命周期恰好是处理请求的时间。因此,您在 doGet(( 中的请求中存储的内容对于 doPost(( 方法来说将不存在。

使用会话属性而不是请求属性。那是在doGet((中:

request.getSession().setAttribute("foo", foo);

在 doPost(( 中做:

Foo notUpdatedFoo = (Foo) request.getSession().getAttribute("foo");

顺便说一句,这将起作用,但它不是线程安全的。

我 100% 确定我明白你的意思,但是:

如果该 servlet 首先由 GET 请求(请求 1(调用,然后转到 JSP。从那个 JSP 你再次调用 servlet?如果是这样,这是一个新请求(请求 2(,您的属性将不会出现在新请求上。

为此,您必须调整 JSP 以查看该属性是否存在并将其传递给新请求。

或者您必须使用 sesion 变量。

我希望这是您的情况,并且您不会在相同的请求之后尝试访问doGet((和doPost((。

我认为您应该将method属性添加到<form>标签中:

<form ..... method="POST">

然后提交表单将调用 servlet 的 doPost() 方法。

相关内容

  • 没有找到相关文章

最新更新