Java 请求的资源在Servlet、Tomcat7中不可用

Java 请求的资源在Servlet、Tomcat7中不可用,java,tomcat,servlets,deployment,Java,Tomcat,Servlets,Deployment,我知道这个问题已经被问了很多次,也得到了回答。但是,我无法解决这个问题。我已经尝试了web.xml中所有可能的映射方法。还使用了@WebServlet注释。在提交html表单后,我仍然无法转到servlet。还尝试更改服务器位置。请帮忙 Please find my web.xml, login page and servlet. <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.

我知道这个问题已经被问了很多次,也得到了回答。但是,我无法解决这个问题。我已经尝试了web.xml中所有可能的映射方法。还使用了@WebServlet注释。在提交html表单后,我仍然无法转到servlet。还尝试更改服务器位置。请帮忙

Please find my web.xml, login page and servlet.

<?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>Webservice</display-name>
  <servlet>

        <servlet-name>MyServlet</servlet-name>
        <servlet-class>controller.ItemServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/ItemServlet</url-pattern>
    </servlet-mapping>


  <welcome-file-list>
    <welcome-file>html/Login.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>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
<!--  <link rel="stylesheet" TYPE="text/css" href="../css/mystyle.css" ></link>
<link href='http://fonts.googleapis.com/css?family=Lily+Script+One' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../js/Validations.js"></script>-->


</head>
<body>
<div id='header'>Online Music Store</div>

<div id='content'>
<form name="UserLogin" action="./ItemServlet?req=Login" method="post">
User Id <input type="text" name="userId" ></input>
Password<input type="password" name="password" ></input>
<input type="submit" value="Login"></input>
</form>

</div>

</body>
</html>


    /**
     * Servlet implementation class ItemServlet
     */
    //@WebServlet("/ItemServlet")
    public class ItemServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public ItemServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //response.getWriter().append("Served at: ").append(request.getContextPath());
            doPost(request,response);
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub

            CDDao daoObj= new CDDao();
            if("Login".equalsIgnoreCase(request.getParameter("req")))
            {
            System.out.println("Inside if");
            System.out.println(request.getParameter("req"));
请找到my web.xml、登录页面和servlet。
介绍
MyServlet
controller.ItemServlet
MyServlet
/ItemServlet
html/Login.html
index.htm
index.jsp
default.html
default.htm
default.jsp
主页
在线音乐商店
用户Id
密码
/**
*Servlet实现类ItemServlet
*/
//@WebServlet(“/ItemServlet”)
公共类ItemServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*@参见HttpServlet#HttpServlet()
*/
公共ItemServlet(){
超级();
//TODO自动生成的构造函数存根
}
/**
*@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
//response.getWriter().append(“服务于:”).append(request.getContextPath());
doPost(请求、响应);
}
/**
*@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
//TODO自动生成的方法存根
CDDao daoObj=新的CDDao();
if(“Login”.equalsIgnoreCase(request.getParameter(“req”))
{
System.out.println(“内部if”);
System.out.println(request.getParameter(“req”);

像这样更改您的
web.xml

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>packagename.ItemServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/ItemServlet</url-pattern>
</servlet-mapping>
重新启动服务器并检查是否有问题。 另外,将此行更改为

<form name="UserLogin" action="ItemServlet" method="post">

这一行有问题。我相信这是您的
登录.html
,它位于
html
文件夹中。当您使用
/
时,它是一个相对url,在
html/ItemServlet
位置查找
ItemServlet

<form name="UserLogin" action="./ItemServlet?req=Login" method="post">

我不知道你的完整文件夹结构,但可能这应该适合你

<form name="UserLogin" action="../ItemServlet?req=Login" method="post">

或者提供完整的上下文

<form name="UserLogin" action="/[YOUR WEB CONTEXT]/ItemServlet?req=Login" method="post">


感谢您提供的解决方案。我以前尝试过。这也不起作用。但今天我使用./self获得了输出,没有任何更改。此错误介于两者之间。不确定原因。我已经给出了包名,即“controller”.controller.ItemServlet是我的servlet类。此表单操作不起作用。正如我在上一篇评论中所说的,此错误昨天在没有任何代码更改的情况下消失。我获得了成功的输出。今天错误再次出现。我想这是否是tomcat的问题。感谢您的回复。。
<form name="UserLogin" action="/[YOUR WEB CONTEXT]/ItemServlet?req=Login" method="post">