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>