1

There are a million of these but none I have looked at so far have been relevant to me- sorry in advance if this is redundant.

Anyway, I'm trying to create a jsp page that reads a CSV file and displays it as a table and right now I'm trying to make a submit button that takes the fields from two text boxes and appends them to the csv file.

I have created the servlet and the jsp, but when I click the submit button on my JSP (when running it as a dynamic web project in Eclipse with TomCat) it gives me the 404 screen with:

HTTP Status 404 - /CSVTest/CSVFile


type Status report

message /CSVTest/CSVFile

description The requested resource (/CSVTest/CSVFile) is not available.

I have tried looking at the xml file to see if it is messed up but I can't figure it out. I am really inexperienced with jsp and whatnot so all I know is basically research over the past two days trying to figure out how to complete this project. Any ideas as to why I'm getting this error?

Here's the xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
    <display-name>CSVTest</display-name>
    <servlet>
        <servlet-name>CSVFile</servlet-name>
        <servlet-class>org.tiaa.csv.CSVFile</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CSVFile</servlet-name>
        <url-pattern>/CSVFile</url-pattern>
    </servlet-mapping>
    <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>

Here's the servlet:

package org.tiaa.csv;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CSVFile extends HttpServlet implements Servlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("button1") != null) {
               FileWriter fw = new FileWriter("c:\\csv\\myfile.csv"); 
               PrintWriter out = new PrintWriter(fw);
               String fname = request.getParameter("fname");
               String lname = request.getParameter("lname");
               out.print(lname);
               out.print(",");
               out.print(fname);
        }

        request.getRequestDispatcher("/csvreader.jsp").forward(request, response);
    }
}

And here's the JSP

<%@ page import="java.io.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% 
   String fName = "c:\\csv\\myfile.csv";
   String thisLine; 
  int count=0; 
  FileInputStream fis = new FileInputStream(fName);
  DataInputStream myInput = new DataInputStream(fis);
  int i=0; 
%>
<table>
<%
while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(",");
for(int j=0;j<strar.length;j++)
{
if(i!=0)
{
out.print(" " +strar[j]+ " ");
}
else
{
out.print(" <b>" +strar[j]+ "</b> ");
}
} 
out.println("<br>");
i++;
} 
%>
</table>
Last name:<input type='text' name='lname' /> First name:<input type='text' name='fname' />
<form action="CSVFile" method="post">
    <input type="submit" name="button1" value="Submit" />
</form>
</body>
</html>
  • In what URL did you run that .jsp? – acdcjunior Jul 10 '13 at 03:51
  • The url for it in eclipse is http://localhost:8080/CSVTest/csvreader.jsp – user2566105 Jul 10 '13 at 03:57
  • Where did you place your jsp? – Bnrdo Jul 10 '13 at 04:05
  • In the Eclipse IDE the jsp is under the "WebContent" folder. – user2566105 Jul 10 '13 at 04:08
  • There's nothing seems to be wrong in the codes you posted. I even created a new project and I successfully run it. Did you try cleaning the project and the server as well? – Bnrdo Jul 10 '13 at 04:16
  • In tomcat, I believe, there is an access filter logging (turned off by default). Turn it on, it will record all incoming requests- you can check how your request URI is coming to the server. – Bimalesh Jha Jul 10 '13 at 04:22
  • `CSVTest` is the name of your project no? Also, `/CSVTest` is the path set in the tomcat web modules? – Bnrdo Jul 10 '13 at 04:23
  • I cleaned the project. I restarted the server too. Sorry for my illiteracy but where can I check the path set in the tomcat web modules? – user2566105 Jul 10 '13 at 04:41
  • Double click the server in the `Server` tab. In the bottom part click `Modules` tab. Make sure there is an entry for the project you are running (Path=`/CSVTest`, Document Base=`CSVTes`t, Module=`CSVTest`, Auto Reload=`Enabled`) – Bnrdo Jul 10 '13 at 05:56
  • Just checked- it's there. Weird... – user2566105 Jul 10 '13 at 06:12

0 Answers0