简体   繁体   English

Java Servlet-导航到页面

[英]Java servlets - navigate to page

I have the following project structure: 我有以下项目结构:

在此处输入图片说明

I am trying to navigate to concerts.jsp from index.jsp : 我试图从导航到的index.jsp concerts.jsp:

index.jsp 的index.jsp

<li class="active"><a href="/ConcertsController">Concerts</a></li>

This is my ConcertsController: 这是我的ConcertsController:

@WebServlet("/ConcertsController")
public class ConcertsController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ConcertsController() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        request.getRequestDispatcher("/WEB-INF/pages/concerts.jsp").forward(request, response);
    }
        ...
    }

When I run the app, and click on the link in index.jsp , localhost:8080/ConcertsController URL opens and I get the 当我运行应用程序,然后单击index.jsp中的链接时, localhost:8080 / ConcertsController URL打开,我得到了

The requested resource is not available 所请求的资源不可用

error. 错误。

Edit 1: I am using Tomcat v7.0, servlets v3.0 and Java 8. 编辑1:我正在使用Tomcat v7.0,Servlet v3.0和Java 8。

Edit 2 (possible duplicate): Well, surely I need to be able to do this without using the jstl library. 编辑2(可能重复):好吧,当然,我需要能够在不使用jstl库的情况下做到这一点。

Edit 3: When localhost:8080/ConcertsController opens, I get the "resource not found" error. 编辑3:localhost:8080 / ConcertsController打开时,出现“找不到资源”错误。 But when I manually edit the url to localhost:8080/AppName/ConcertsController , it works. 但是,当我手动将URL编辑为localhost:8080 / AppName / ConcertsController时 ,它可以工作。

URL localhost:8080/ConcertsController means Application name is ConcertsController , port number is 8080 and the application is run locally. URL localhost:8080/ConcertsController表示应用程序名称为ConcertsController ,端口号为8080并且该应用程序在本地运行。 But you do not have ConcertsController Application. 但是您没有ConcertsController应用程序。 so you are getting error 所以你出错了

The requested resource is not available 所请求的资源不可用

There are 2 solutions 有2个解决方案

  1. if you are not using JSTL. 如果您不使用JSTL。

Change <a href="/ConcertsController"> to <a href="<%${pageContext.request.contextPath}%>/ConcertsController"> 更改<a href="/ConcertsController"><a href="<%${pageContext.request.contextPath}%>/ConcertsController">

  1. If JSTL is used then <c:set var="contextPath" value="${pageContext.request.contextPath}" /> and then change <a href="/ConcertsController"> to <a href="${contextPath}/ConcertsController"> 如果使用了JSTL,则<c:set var="contextPath" value="${pageContext.request.contextPath}" /> ,然后将<a href="/ConcertsController"> to <a href="${contextPath}/ConcertsController">更改<a href="/ConcertsController"> to <a href="${contextPath}/ConcertsController">

Use 采用

           <a href="${pageContext.request.contextPath}/ConcertsController">

Also when forwarding the request to cencerts.jsp use ServletContext.getRequestDispatcher(....) 另外,将请求转发到cencerts.jsp时,请使用ServletContext.getRequestDispatcher(....)

ServletRequest.getRequestDispatcher() method will evaluate the path relative to the path of the request. ServletRequest.getRequestDispatcher()方法将评估相对于请求路径的路径。 But for the method of ServletContext, the path parameter cannot be relative and must start with /( / means at the root of web application not current relative path). 但是对于ServletContext的方法,path参数不能是相对的,并且必须以/(/表示在Web应用程序的根目录而不是当前的相对路径)开头。

Try using the following: 尝试使用以下内容:

request.getRequestDispatcher("/pages/concerts.jsp").forward(request, response);

instead of: 代替:

request.getRequestDispatcher("/WEB-INF/pages/concerts.jsp").forward(request, response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM