我创建了一个在 Eclipse 上工作但给了我的 servlet
“请求的资源不可用。”
在http://cs3.calstatela.edu:8080/cs3220stu48/Labs/RequestSummary在我的 tomcat 服务器上访问 时,在 Eclipse http://localhost:8080/cs3220stu48/Labs/RequestSummary上仍然有效 这是我的 Servlet 代码
package CS3220;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;
@WebServlet("/Labs/RequestSummary")
public class RequestSummary extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Date date = new Date();
out.println("<!DOCTYPE html>");
out.println("<html lang =\"en\">");
out.println(" <head>");
out.println(" <link rel= \"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">");
out.println(" <meta charset = \"UTF-8\">");
out.println(" <title>Request Summary</title>");
out.println(" </head>");
out.println(" <body>");
out.println( "<div class=\"container\">");
out.println(" <div class =\"jumbotron\">");
out.println(" <h1>Request Summaray</h1>");
out.println(" <p>");
out.println( "The following " + "<code>" + request.getMethod() + "</code>" + " request was sent on " + "<code>" + date.toString() + "</code>");
out.println(" </p>");
out.println(" </div>");
out.println( "<h3>Request Parameters</h3>");
out.println( "<table class = \"table table-bordered table-striped table-hover\">");
out.println( "<thead>");
out.println( "<tr>");
out.println( "<td>Parameter Name</td>");
out.println( "<td>Parameter Value</td>");
out.println( "</tr>");
out.println( "</thead>");
Map<String, String[]> Map=request.getParameterMap();
for(String key:Map.keySet()){
String[] Parameter=(String[])Map.get(key);
out.println("<tr>");
out.println("<td>" + key + "</td>");
out.println( "<td>");
for(String value:Parameter){
out.println("<span class=\"label label-info\">"+ value + "</span>");
}
out.println("</td>");
out.println("</tr>");
}
out.println( "</table>");
out.println( "<h3>Header Information</h3>");
out.println( "<table class = \"table table-bordered table-striped table-hover\">");
out.println( "<thead>");
out.println( "<tr>");
out.println( "<td>Header Name</td>");
out.println( "<td>Header Value</td>");
out.println( "</tr>");
out.println( "</thead>");
Enumeration<String> e = request.getHeaderNames();
while(e.hasMoreElements()) {
String name = e.nextElement();
String value = request.getHeader(name);
out.println("<tr>");
out.println("<td>" + name + "</td>");
out.println("<td>"+ value + "</td>");
out.println("</tr>");
}
out.println( "</table>");
out.println(" </div>");
out.println(" </body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
还有我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>cs3220stu48</display-name>
<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>
一些同学告诉我,我不应该修改我的 xml 文件,只需要调整我的 url 模式,但是我不太确定如何处理它。我也确定我将它上传到正确的目录。servlet 放入 web-inf/classes 文件夹,xml 放入 web-inf。这是我第一次使用 servlet,希望能得到一些帮助以及对未来的一些建议。
编辑:我也意识到我的代码,我放置我的java文件的地方可能是问题所在。我把 web.xml 放在 WEB-INF 和 RequestSummary.java 放在 WEB-INF/classes
编辑2:我的构建文件夹在Eclipse上似乎是空的,我相信那里应该有一个类文件夹,我检查了我的构建路径,它确实导致构建/类默认