Servlet无法转发-Servlet异常 [英] Servlet not forwarding - Servlet exception
问题描述
我的servlet无法正确转发.我不断尝试Tomcat-8.5 404或500错误页面.当我收到500错误时,它只是说servlet抛出了一个异常.
My servlet is not forwarding correctly. I keep getting forwarded to the Tomcat-8.5 404 or 500 error pages depending what I try. When I get the 500 error, it just says the servlet threw an exception.
我正在尝试检索定位标记的名称(在导航栏中),然后根据该名称将其转发到正确的JSP.一旦开始工作,我将使用该servlet检查现有会话或启动新会话.
I'm attempting to retrieve the name of an anchor tag (in a nav bar), and based on the name forward it to the correct JSP. Once I get this working I will use this servlet to check for existing sessions or start new ones.
非常感谢您的帮助.
下面是我的servlet映射:
Below is my servlet mapping:
<servlet>
<servlet-name>SessionTracker</servlet-name>
<servlet-class>SessionTracker</servlet-class>
</servlet>
<!-- Servlet Maps -->
<servlet-mapping>
<servlet-name>SessionTracker</servlet-name>
<url-pattern>/donate/*</url-pattern>
</servlet-mapping>
以下是定位标记:
<li><a href="/donate/donate.jsp" name="donate">Donate</a></li>
请注意,我还在锚标记上尝试了以下方法:
Note, I have also tried the following on the anchor tag:
<li><a href="/donate/donate.jsp?name=donate" name="donate">Donate</a</li>
下面是相应的Java函数:
Below are the respective Java functions:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext sc = getServletContext();
String url = forwardRequest(request, response, sc);
sc.getRequestDispatcher(url).forward(request, response);
}
private static String forwardRequest(HttpServletRequest request, HttpServletResponse response, ServletContext sc){
String url = new String("");
String name = request.getParameter("name");
switch(name){ //switch state to determine which
case "donate":
url = "/donate/donate.jsp";
break;
case "mission":
url = "/about/missionStatement.jsp";
break;
}
return url;
}
推荐答案
好,让我们分析一下代码:
Ok Let's analyze your code:
1.- dd(web.xml)
1.- dd (web.xml)
<servlet-class>SessionTracker</servlet-class>
尽量不要使用默认软件包(尽管这里没有问题)
Try not to use Default package, (Although there is no problem here)
<url-pattern>/donate/*</url-pattern>
哦,男孩,这是个问题,您是在对容器说,嘿,如果有人使用url
Oh Boy, here is a problem, you're saying to the Container, HEY, If someone uses the url http://localhost:8080/mysite/donate/whateverIDon'tCare call the Servlet SessionTracker, so I can use this diferents paths and it will call the same Servlet
/mysite/donate/some
/mysite/donate/hereWeGo
/mysite/donate/lol
这不好,请尝试将其更改为
So that is not good, try to change it to
<url-pattern>/donate/SesionTrackerServlet</url-pattern>
2.-看看您的
<li><a href="/donate/donate.jsp?name=donate" name="donate">Donate</a</li>
您看到失败了吗?是的,正如我所说,当用户单击此处时,它将调用您的Servlet,因此您的Servlet将调用您的方法forwardRequest,您认为会发生什么?是的,它将转向url ="/donate/donate.jsp";但是等等,你看到我的观点1了吗?您将再次调用servlet,并再次调用方法,并一次又一次地调用booooom....Estado HTTP 500-Servlet执行引发了异常,因为您创建了循环.
can you see the failure? yes as I said , when a user click here it will call your Servlet, so your servlet will call your method forwardRequest and What do you think will happen? yes it will foward to url = "/donate/donate.jsp"; but wait, did you see my point 1? your will call again the servlet and again the method and again and again and again and booooom....Estado HTTP 500 - Servlet execution threw an exception because you made a Loop.
但我对此表示更改:
Web.xml
<servlet>
<servlet-name>SessionTracker</servlet-name>
<servlet-class>SessionTracker</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionTracker</servlet-name>
<url-pattern>/donate/SessionTrackerServlet</url-pattern>
</servlet-mapping>
现在输入此网址(当然,如果需要,请更改端口)
Now enter this url (of course change your port if you need)
http://localhost:8080/Test/donate/SessionTrackerServlet?name = donate
还有woooala =)
And woooala =)
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
[![enter image description here][4]][4]
[1]: https://i.stack.imgur.com/9hhTO.png
[2]: https://i.stack.imgur.com/Hqha7.png
[3]: https://i.stack.imgur.com/uUvn4.png
[4]: https://i.stack.imgur.com/Tge2x.png
这篇关于Servlet无法转发-Servlet异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!