gpt4 book ai didi

java - servlet doGet通过servlet注解调用启动两次

转载 作者:行者123 更新时间:2023-11-28 22:56:13 24 4
gpt4 key购买 nike

由于某些原因,我的 servlet doGet 方法被调用了两次。我通过使用 servlet 注释“/Profile/*”从 index.jsp 页面调用 servlet。并将路径作为 localhost:1919/Profile/username 传递给 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

非常感谢您的提前帮助!

最佳答案

好的,所以问题与您映射 servlet 的方式以及您希望访问页面的方式有关。

因此您的根网址是:http://localhost:1919/

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

此时您在浏览器中的 url 如下所示:http://localhost:1919/Profile/xyz .现在,您单击链接以更新用户,浏览器会附加 update_profile.jsphttp://localhost:1919/Profile/ , 所以 url 看起来像 http://localhost:1919/Profile/update_profile.jsp , 但问题是,它仍然以 /Profile 开头并且仍然符合您在web.xml 中定义的servlet 映射 .. 所以显然调用了 servlet..

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

编辑:here也是 / 的一个很好的例子应使用网址。

关于java - servlet doGet通过servlet注解调用启动两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26548028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com