gpt4 book ai didi

Java Servlet RandomAccessFile 错误

转载 作者:行者123 更新时间:2023-11-28 22:50:59 27 4
gpt4 key购买 nike

我收到错误“HTTP Status 404-/servlet/FormPost类型状态报告消息/servlet/FormPost描述请求的资源不可用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>");
}
}

最佳答案

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

关于Java Servlet RandomAccessFile 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com