Natela Natela - 2 months ago 11
Java Question

Javaservlet RandomAccessFile errors

I'm receiving the error "HTTP Status 404-/servlet/FormPost
type Status report
message /servlet/FormPost
description The requested resource is not available
Apache Tomcat/7.0.72"

Can anyone see what is wrong with my code? I'm using Eclipse and Tomcat v7.0.
I'm trying to get the program to output a form, write to a random access file, and return the results from the random access file. I'm a SQL programmer, not a fan of Java servlets, and don't really understand them very well. Please explain as if I was new to programming, or edit my code. Thank you!

Here is my code:

package net.codejava;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name="FormPost", urlPatterns={"/FormPost"})

public class FormPost extends HttpServlet {

private static final long serialVersionUID = 1L;

private String target = "localhost:7070";

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

printHeader(out);
printForm(out);
printFooter(out);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
final int BUFFER_LENGTH = 41;
String data = null;
PrintWriter out = response.getWriter();
RandomAccessFile randomAccessFile = new RandomAccessFile("C:/temp/week4.dat", "rw");

StringBuffer buffer = new StringBuffer(request.getParameter("myName"));
StringBuffer buffer2 = new StringBuffer(request.getParameter("myAddress"));
StringBuffer buffer3 = new StringBuffer(request.getParameter("myCityAndState"));
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.writeBytes(buffer.toString());
randomAccessFile.seek(0);
printForm(out);

buffer.setLength(BUFFER_LENGTH);
buffer.setCharAt(BUFFER_LENGTH - 1, '\n');

randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.writeChars(buffer.toString());
randomAccessFile.writeChars(buffer2.toString());
randomAccessFile.writeChars(buffer3.toString());
randomAccessFile.seek(0);

while ((data = randomAccessFile.readLine()) != null){
out.println(data + "<br />");
randomAccessFile.close();
doPost(request, response);
}
printFooter(out);
}

public void printHeader(PrintWriter out){
out.println("<!DOCTYPE html>");
out.println("<html lang='en'>");
out.println("<title>");
out.println("Servlet Read Write File");
out.println("</title>");
out.println("<body>");
out.println("<div>");
}

public void printFooter(PrintWriter out){
out.println("</div>");
out.println("</body>");
out.println("</html>");
}

public void printForm(PrintWriter out){
out.println("<form method='post' action='http://" + target + "/MyServlet/FormPost'>");
out.println("<label>Enter Name</label>&nbsp;&nbsp;&nbsp;");
out.println("<input type='text' name='myName' size='30' maxlength='30'/>");
out.println("<input type='submit' />");
out.println("<label>Enter Address</label>&nbsp;&nbsp;&nbsp;");
out.println("<input type='text' name='myAddress' size='30' maxlength='30'/>");
out.println("<input type='submit' />");
out.println("<label>Enter City and State</label>&nbsp;&nbsp;&nbsp;");
out.println("<input type='text' name='myCityAndState' size='30' maxlength='30'/>");
out.println("<input type='submit' />");
out.println("</form>");
}
}

yvs yvs
Answer

The problem does not related to your implementation of methods.
HTTP 404 tells you that tomcat http-server can not find any handler for your request. In a simple case, your request should looks like "localhost:8080/<your_war_file_/_app_name>/<string_matching_your_web_servlet_pattern>"