gpt4 book ai didi

java - 从 Servlet 中的 java 类获取属性(在 JSP 中看不到转发的请求)

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

<分区>

我正在研究 Head First Servlets 和 JSP,但一直被这个问题困扰。该主题从本书的第 480 页开始。我正在尝试开发没有 IDE 的 servlet。此 servlet 应以表格形式显示电影列表。

这是我的代码电影列表.java:

package com.model;
import javax.servlet.*;
import javax.servlet.http.*;

public class MovieList extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res){
String[] movieList = {"Hannah Montana", "Kill Bill", "Terminator 2"};
req.setAttribute("movieList", movieList);
}
}

显示.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html><body>
<strong> Movie list:</strong>
<br><br>

<table>
<c:forEach var="movie" items="${movieList}">
<tr>
<td>${movie}</td>
</tr>
</c:ForEach>
</table>

</html></body>

Tomcat8 在浏览器中显示的内容:

type Exception report

消息:绝对 uri:http://java.sun.com/jsp/jstl/core无法在 web.xml 或随此应用程序部署的 jar 文件中解析

描述:服务器遇到内部错误,无法完成此请求。

异常:

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:55)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:277)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:75)
org.apache.jasper.compiler.TagLibraryInfoImpl.generateTldResourcePath(TagLibraryInfoImpl.java:243)
org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:124)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:411)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:469)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1430)
org.apache.jasper.compiler.Parser.parse(Parser.java:139)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

注意 Apache Tomcat/8.0.24 日志中提供了根本原因的完整堆栈跟踪。

我无法设法将代码从我的 java 类绑定(bind)到 jsp。我错过了 DD 还是?

Now I solved it, thanks a lot!
I have got nex question:
It renders only text "Movie list:" and nothing more
How I fix that?

现在我更改了我的 servlet 代码,以便它将请求转发到 Show.jsp 并重新编译我的类,重新启动 tomcat,但它仍然无法工作...

req.setAttribute("movieList", movieList);


try {

getServletConfig().getServletContext().getRequestDispatcher(

"Show.jsp").forward(req ,res );



} catch (ServletException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

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