1

In my web app the welcome page needs to show a table. However it (http://localhost:8080/MyApp ) shows "null" , but if I add 'ServlentToRead.java' I mean, http://localhost:8080/MyApp/ServlentToRead it displays the table.

Why I can't get the table as first page ? here is the 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>

edit:

<?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>
student
  • 37
  • 6

1 Answers1

0

Why I can't get the table as first page ?

change your web.xml to this:

<?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>

You will now see the table whenever you visit the url:

http://localhost:8080/MyApp

EDIT: sorry, remove the forward slash from in your web.xml:

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

so it looks like this:

<welcome-file>ServlentToRead</welcome-file>
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • HTTP Status 404- Not Found: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists...Why? – student May 23 '18 at 00:24
  • Thanks a lot it works!!!!!! Does it mean I don't need the file index.jsp? – student May 23 '18 at 00:30
  • good to hear. No you still need the index.jsp file. The Servlet with the url mapping "ServlentToRead" forwards the request to that index.jsp file. Feel free to upvote or mark as the correct answer if it helped. – Jonathan Laliberte May 23 '18 at 00:34