Javaservlet RandomAccessFile错误

时间:2016-09-24 23:04:58

标签: java eclipse tomcat servlets

我收到错误“HTTP Status 404- / servlet / FormPost 类型状态报告 message / servlet / FormPost description请求的资源不可用 Apache Tomcat / 7.0.72“

任何人都可以看到我的代码有什么问题吗?我正在使用Eclipse和Tomcat v7.0。 我正在尝试让程序输出一个表单,写入随机访问文件,并从随机访问文件返回结果。我是一名SQL程序员,不是Java servlet的粉丝,也不是很了解它们。请解释好像我是编程新手,或编辑我的代码。谢谢!

这是我的代码:

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>");
     }
}

1 个答案:

答案 0 :(得分:1)

问题与您的方法实施无关 HTTP 404告诉您tomcat http-server找不到您的请求的任何处理程序。 在简单的情况下,您的请求应该是"localhost:8080/<your_war_file_/_app_name>/<string_matching_your_web_servlet_pattern>"