请求的资源在Servlet,Tomcat7

时间:2016-03-13 16:02:29

标签: 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.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"));

2 个答案:

答案 0 :(得分:0)

像这样更改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>

你的servlet类为

@WebServlet(name="MyServlet", urlPatterns={"/ItemServlet"})
public class ItemServlet extends HttpServlet {

重新启动服务器并检查是否存在问题。 另外,将此行更改为

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

答案 1 :(得分:0)

问题出在这一行。我相信这是Login.html文件夹中的html。当您使用./时,它是一个相对网址,正在位置ItemServlet

中寻找html/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">