gpt4 book ai didi

java - HTTP 状态 404 错误 Java Servlet

转载 作者:行者123 更新时间:2023-11-30 04:32:43 25 4
gpt4 key购买 nike

问题是“请求的资源(/main_servlet/)不可用。”

源代码如下:

上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/main_servlet"/>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">


<servlet>
<servlet-name>main_servlet</servlet-name>
<servlet-class>smart_servlet.main_servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>main_servlet</servlet-name>
<url-pattern>/main_servlet</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

</web-app>

main_servlet.java:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package smart_servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Box
*/
public class main_servlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

out.println("<html>");
out.println("<head>");
out.println("<title>Servlet main_servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet main_servlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}
}

main_servlet.java、context.xml 和 web.xml 的代码仅由 NetBeans 自动生成。我真的不明白为什么找不到来源。非常感谢!

最佳答案

尝试使用以下 URL 访问您的代码:

http://(servername)/main_servlet/main_servlet 

where (severname) is the name of the server on which your app is running.

这是必要的,因为您已将应用程序上下文和 servlet URL 模式定义为“/main_servlet”

或者 - 更好的解决方案是将应用程序上下文更改为其他内容,例如“MyApp”

<Context antiJARLocking="true" path="/MyApp"/>

然后您将使用该 URL

http://(servername/MyApp/main_servlet

关于java - HTTP 状态 404 错误 Java Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14272172/

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