【问题标题】:Can't display the table when running index.jsp运行 index.jsp 时无法显示表格
【发布时间】:2018-11-01 19:50:10
【问题描述】:

在我的网络应用中,欢迎页面需要显示一个表格。但是它 (http://localhost:8080/MyApp ) 显示 "null" ,但如果我添加 'ServlentToRead.java' 我的意思是,http://localhost:8080/MyApp/ServlentToRead 它会显示表格。

为什么我不能将表格作为第一页?这是 index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%
    String table=(String) request.getAttribute("table");

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Mytable</title>

</head>

<body>

<h1>Mytable</h1>

<%= table %>

</body>
</html>

编辑:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>MyApp</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

【问题讨论】:

  • 你用什么做服务器?雄猫?
  • 您在另一个 url 上看到表的原因是因为它映射到了一个 Servlet。如果您希望它也不是 null,则需要将欢迎文件映射到该 servlet。请发布您的 web.xml
  • 是的,我确实使用 Tomcat
  • 您需要更改应用程序的上下文根。默认情况下,它是 servlet 名称。要更改的内容取决于您是否在 Eclipse 中单独运行它。签出这个答案stackoverflow.com/questions/2437465/…
  • 如果你是单独部署它,请将战争名称设置为 ROOT.war

标签: java jsp web-applications


【解决方案1】:

为什么我不能将表格作为第一页?

将您的 web.xml 更改为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>MyApp</display-name>
  <welcome-file-list>
    <welcome-file>ServlentToRead</welcome-file>
  </welcome-file-list>
</web-app>

现在,每当您访问该网址时,您都会看到该表格:

http://localhost:8080/MyApp

编辑:抱歉,请从您的 web.xml 中删除正斜杠:

<welcome-file>/ServlentToRead</welcome-file>

所以它看起来像这样:

<welcome-file>ServlentToRead</welcome-file>

【讨论】:

  • HTTP 状态 404- 未找到:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示...为什么?
  • 非常感谢它有效!!!!!!这是否意味着我不需要文件 index.jsp?
  • 很高兴听到。不,您仍然需要 index.jsp 文件。具有 url 映射“ServlentToRead”的 Servlet 将请求转发到该 index.jsp 文件。如果有帮助,请随意点赞或标记为正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 2012-12-28
  • 2013-02-25
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
相关资源
最近更新 更多