Java – Servlet 404错误
我是servlet编程的新手。
以下是我的示例servlet代码
Sample.Java
public class Sample extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); String name=req.getParameter("name");//will return value pw.println("Welcome "+name); pw.close(); } }
在web.xml
sampleservlet Sample 1 sampleservlet /Sample
的index.html
Untitled Document
我的问题
当我右键单击我的项目,然后单击Run as -> Run on server
,我选择服务器,它要求我选择一个服务器,然后我选择要运行的服务器。 在eclipse窗口中打开一个浏览器窗口,其地址URL为http:// localhost:8080 / Sample /
当我点击login
它给我一个这样的错误,
HTTP状态404 – /示例/欢迎
类型状态报告
留言 /样品/欢迎
description请求的资源不可用。
Apache Tomcat / 7.0.62
截图
为什么我会收到这样的错误?
其他详情
服务器:apache-tomcat-7.0.62 IDE:Eclipse Java EE kepler
我尝试解决问题的步骤
我试过这些,
- 尝试运行servlet时出现HTTP状态404错误
结果
我没有在WEB-INF
文件夹中看到.class
文件。
- Tomcat给servlet带来404错误
结果在此路径“tomcat7 \ work \ Catalina \ localhost \ your app”中, work
文件夹为空。
如果我需要发布任何更多细节,请告诉我。
来自IDE的目录结构
你的代码在tomcat 7和eclipse中对我有用。 也许你的index.html与你发布的那个不一样,因为它的目标是Sample / welcome而不是Sample / Sample。
提示:
- 确保index.html发布的版本与代码中的版本相同。 修改内容并确保其正确发布。
- 直接在浏览器中加载http:// localhost:8080 / Sample / Sample?name = Random ,以查看servlet是否正常运行。
- 双击服务器上的Tomcat7。 转到您的服务器路径。 例如C:\ Java \ workspace.metadata.plugins \ org.eclipse.wst.server.core \ tmp2 \ wtpwebapps \ Sample \ WEB-INF \ classes(你的Sample类应该在那里)仔细检查你是否对这些文件夹有权限。
- 将项目导出为WAR并将其直接部署在tomcat中而不使用eclipse。
- 清理/构建您的项目
- 在Servers / server.xml下的Package Explorer中,您应该有一个声明的上下文,例如
如果你不仔细检查这些文件夹中的permisions。 再次从头开始添加tomcat7服务器。
试试这个,你不需要为任何servlet编写XML文件;)
只需添加注释@WebServlet("/urlpattern")
并在JSP中从
调用此URL模式
只有你在Eclipse上工作。
@WebServlet("/Sample") public class Sample extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); String name = req.getParameter("name"); //will return value pw.println("Welcome "+name); pw.close(); }}
希望能帮助到你。