重定向时,请求的资源(/xxx.html)不可用 [英] The requested resource (/xxx.html) is not available when redirect

查看:200
本文介绍了重定向时,请求的资源(/xxx.html)不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在做功课,以找出原因,但我仍在努力.请帮忙!

I really do my homework to figure out why but I am still struggling. Please help!

我有一个运行良好的Servlet,今天之前没有任何页面(t​​omcat 7和eclipse)

I have a Servlet which is running well without any page before today(tomcat 7 & eclipse)

现在,我希望尝试添加一些页面并重定向到它们.我加了一行

Now, I hope to try add some pages and redirect to them. I added a line of

response.sendRedirect("/userhelp.html");

要测试页面是否正常工作,否则不能正常工作.

To test if the page is working, but it is not.

我将userhelp.html放在WebContent的下面.为了进行测试,我尝试将其复制到WEB-INF中,但两者均无效.

I place my userhelp.html under foler of WebContent. For test, I have tried copying it into WEB-INF, neither of it works.

如果我将"/userhelp.html"替换为"userhelp.html",则该页面没有错误页面,但具有递归重定向".

And If I replace "/userhelp.html" into "userhelp.html" it did not have a error page but have "recursive redirect".

下面是我的相对代码.

@WebServlet(name="core",urlPatterns={"/"})
public class CoreServlet extends HttpServlet {


protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    response.sendRedirect("/userhelp.html");    

推荐答案

您需要将sendrederct编辑为:

You need to edit your sendrederct to :

response.sendRedirect("/ProjectName/userhelp.html");

response.sendRedirect("/ProjectName/userhelp.html");

您有response.sendRedirect("/userhelp.html");对sendRedirect的争论以正斜杠开头.这意味着相对于容器的根".因此,容器会建立相对于请求原始URL的URL.

You have response.sendRedirect("/userhelp.html"); arguement to your sendRedirect starts with a forward slash.Which means that 'realative to root of container'.So container builds URL relative to original URL of request.

您的userhelp.html页面位于项目的webcontent中.它从容器根目录的相对路径应为/Projectname/userhelp.html.因此您需要在 response.sendRedirect.

Your userhelp.html page exits in your project's webcontent.It's relative path from container's root should be /Projectname/userhelp.html.So you need to edit your path in response.sendRedirect.

问题出在您的servlet映射中,因为@servletmapping(/)意味着与此url模式(/)有关的所有内容都应发送到该servlet.所以真正发生的是,当您的servlet首次被调用时然后调用response.sendredirect(),创建一个新的请求对象,并将其作为/的路径,因此再次调用您的servlet,并且此递归过程开始.

Edit : Problem is in your servlet mapping as @servletmapping(/) means that anything which comes for this url pattern(/) , should be send to this servlet.So what really happens is that when your servlet is called first time and response.sendredirect() is called ,a new request object is created and as path for it is / , So again your servlet gets called and this recursive process starts.

因此,解决方案是将servlet映射更改为:@servletmapping(/test),然后通过调用yourlocalhost/test/来调用此servlet.因此,当此时间重定向完成时,就不会为此调用Servlet.

So solution for this is to change servlet mapping to : @servletmapping(/test) and then call this servlet by calling yourlocalhost/test/ .So when this time redirection is done , Servlet is not called for that.

这篇关于重定向时,请求的资源(/xxx.html)不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆