The requested resource (/xxx.html) is not availabl

2019-08-12 07:26发布

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

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.

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

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

Below is my code which is relative.

@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");    

1条回答
何必那么认真
2楼-- · 2019-08-12 08:19

You need to edit your sendrederct to :

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

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.

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.

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.

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.

查看更多
登录 后发表回答