servlet doGet通过servlet注释调用启动两次

时间:2014-10-24 12:37:36

标签: java jsp tomcat servlets intellij-idea

由于某些原因,我的servlet doGet方法被调用两次。 我通过使用servlet注释" / Profile / *从index.jsp页面调用servlet。它将路径作为localhost:1919 / 配置文件/用户名传递给getPost方法。

<li><a href="Profile/<%=lg.getUsername()%>">Your Profile</a></li>

Servlet Profile从java容器中检索数据,将其存储为属性并转发到profile.jsp页面。

rd = request.getRequestDispatcher("/profile.jsp");
            request.setAttribute("ProfileInfo", proInfo);

            rd.forward(request, response);

我的profile.jsp页面使用java代码显示所有数据(并且它完美地显示了它)。 但后来我点击链接到下一个update_profile.jsp页面。只是简单的链接。然后我意识到,在这一步上,出于奇怪的原因,我从第二次调用了来自Profile servlet的doGet方法。并将路径作为localhost传递:1919 / Profile / update_profile.jsp

这是我的profile.jsp代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="uk.ac.dundee.computing.aec.instagrim.containers.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Picturize - User Profile</title>
   <!-- <link rel="stylesheet" type="text/css" href="Styles.css"/> -->
</head>
<body>

<header>
    <h2>Picturize - Profile of user</h2>
</header>
<%
    ProfileInfo proInfo = (ProfileInfo) request.getAttribute("ProfileInfo");
%>
<article>

    <h2>User: <%=proInfo.getUsername()%>
    </h2>

    <h3>First name: <%=proInfo.getFirstname()%>
    </h3>

    <h3>Last name: <%=proInfo.getLastname()%>
    </h3>

    <h3>Email address: <%=proInfo.getEmail()%>
    </h3>

    <a href="update_profile.jsp">Update your profile information</a>
</article>
</body>
</html>

update_profile.jsp文件只是带有<h1>hello</h1>输出的简单文件。

我在互联网上看到这可能是映射问题或我的css相对路径连接问题。如你所见,我已经注释掉了我的css连接,问题仍然存在。

IDE中的运行输出也显示以下错误:

org.apache.jasper.JasperException: An exception occurred processing JSP page /profile.jsp at line 27

24: %>
25: <article>
26: 
27:     <h2>User: <%=proInfo.getUsername()%>
28:     </h2>
29: 
30:     <h3>First name: <%=proInfo.getFirstname()%>


Stacktrace:
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:335)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
    at uk.ac.dundee.computing.aec.instagrim.servlets.Profile.displayProfile(Profile.java:116)
    at uk.ac.dundee.computing.aec.instagrim.servlets.Profile.doGet(Profile.java:68)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at org.apache.jsp.profile_jsp._jspService(profile_jsp.java:91)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    ... 38 more

这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
    <description>MySQL Test App</description>

    <servlet>
        <servlet-name>Profile</servlet-name>
        <servlet-class>uk.ac.dundee.computing.aec.instagrim.servlets.Profile</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Profile</servlet-name>
        <url-pattern>/profile</url-pattern>
    </servlet-mapping>

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>picturizedb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

我的tomcat运行配置设置: https://drive.google.com/file/d/0ByZXQ2R3O8B8SERPYzJxZFViLVE/view?usp=sharinghttps://drive.google.com/file/d/0ByZXQ2R3O8B8THE3TGFnUFVpNjg/view?usp=sharing

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

好的,所以这个问题与你映射servlet的方式和你想要访问页面的方式有关。

所以你的根网址是:http://localhost:1919/

首次打开个人资料时(使用此代码<a href="Profile/<%=lg.getUsername()%>">),浏览器可以访问http://localhost:1919/Profile/xyz。这个调用调用servlet,因为它被映射为解析所有以/Profile开头的请求。直到现在一切都像你期望的那样工作。

此时,您在浏览器中的网址如下:http://localhost:1919/Profile/xyz。现在,您点击链接更新用户,浏览器会将update_profile.jsp追加到http://localhost:1919/Profile/,因此网址看起来像http://localhost:1919/Profile/update_profile.jsp,但问题是,它仍然以{{1开头并且仍然符合您在/Profile中定义的servlet映射。因此显然调用了servlet ..

解决方案:
1)在web.xml中定义一个新的servlet,它具有更具体的url,它将处理你的更新请求(检查更多关于servlet url模式匹配here的信息)。
2)如果web.xml路径与Profile之间没有逻辑关系(我认为代码逻辑不太可能),您可以在update_profile.jsp中使用/ (类似于href)会使浏览器向<a href="/update_profile.jsp">而不是http://localhost:1919/update_profile.jsp发出请求。

编辑:here也是如何使用网址中http://localhost:1919/Profile/update_profile.jsp的好例子。