Servlet映射

时间:2014-07-03 08:15:00

标签: servlets web.xml

我的问题是我在web.xml上映射了我的servlet。当我向servlet提交信息时,它给了我“找不到resuorces”错误。我怎么解决这个问题。谢谢你的回答。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<form action="Login" method="POST">
UserName:<input type="text" name="user_name">
<BR>
Password:<input type="password" name="password">
<BR>
<input type="submit" value="Gonder">
<BR>
<input type="reset" value="Reset">
</form>

</body>
</html>

SERVLET CODES就像这样

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String usr_name;
    private String pass;
    static Logger log=Logger.getLogger(Login.class);






    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PropertyConfigurator.configure("C:\\Users\\AliArdaOrhan\\workspace2\\QuizProject\\WebContent\\WEB-INF\\log4j.properties");
        usr_name=request.getParameter("user_name");
        pass=request.getParameter("Password");

        try {
            if(Validation.validate(usr_name,pass)){
                RequestDispatcher rs=request.getRequestDispatcher("Welcome");
                rs.forward(request, response);
            }
            else{
                RequestDispatcher rs=request.getRequestDispatcher("login");
                rs.forward(request, response);
            }
        } catch (ClassNotFoundException | SQLException e) {
            log.error("Cannot Validate User Information", e);
        }

    }

}

WEB XML CODES就像这样

   <?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>QuizProject</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>
  <servlet>
    <description>Login Page</description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>Welcome</display-name>
    <servlet-name>Welcome</servlet-name>
    <servlet-class>Welcome</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Welcome</servlet-name>
    <url-pattern>/Welcome</url-pattern>
  </servlet-mapping>
</web-app>

3 个答案:

答案 0 :(得分:1)

您需要在web.xml中添加Login操作的servlet映射:

<servlet>
    <description></description>
    <display-name>Login</display-name>
    <servlet-name>Login</servlet-name>
    <servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
</servlet-mapping>

确保在web.xml中进行这些更改后重新启动服务器。

如果你使用的是Servlet 3.0,那么你可以在servlet中使用类似上面代码的注释:

@WebServlet("/Login")
public class Login extends HttpServlet {

答案 1 :(得分:0)

您将表单发送到Login(相对网址),但您的servlet正在/GuestBook等待。更改servlet映射或表单的操作属性。

答案 2 :(得分:0)

请在此行中更改

<form action="Login" method="POST">

替换&#34; Login&#34;与&#34; /GuestBook&#34;