我已经在运行tomcat 7.0的实时服务器上通过cpanel部署了Java Web应用程序,它可以正常使用.jsp页面。它似乎没有加载web.xml文件,所以它不映射servlet。
我已在本地测试了我的应用程序。 这是Web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ProcessLogin</servlet-name>
<servlet-class>authentication.ProcessLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProcessLogin</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>
这是login.jsp文件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Here</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div class="header">
...
</div>
<div class="container" style="height:85%">
<div class="contents">
<form name="form1" id="form1" method="post" action="Welcome" >
User Name: <input type="text" name="txtName" id="txtName" required class="txtfield"> <br />
Password:<input type="password" size="50" name="txtPass" required class="txtfield" /> <br />
<input type="submit" value="Sign In" />
</form>
</div>
</div>
<div class="footer">
....
</div>
</body>
</html>
这是index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Management</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="library/jquery-1.9.1.js"></script>
</head>
<body>
<div class="container" style="height:84%">
<div class="contents" style="margin:0 auto; width:650px">
Welcome dear:
</div>
</div>
<div class="footer">
...
</div>
</body>
</html>
AND这是我的“ProcessLogin”servlet ..
package authentication;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
import utilities.User;
@WebServlet(name = "ProcessLogin", urlPatterns = {"/Welcome"})
public class ProcessLogin extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
User user = new User();
String userName = request.getParameter("txtName");
String password = request.getParameter("txtPass");
boolean isUser = user.checkUser(userName,password);
if (isUser) {
HttpSession session = request.getSession();
session.setAttribute("userId", userName);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
} else {
response.sendRedirect("login.jsp?err=1");
}
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
我在搜索此问题时花了十几个小时,但我找不到解决方案。