当我启动loginPage.jsp表单并输入用户名和密码时出现此错误:
type Status report
message /WHFM/LoginServlet.java
description The requested resource is not available.
我缺少什么?我读过有关同一问题的文章,但我觉得区分大小写是正确的,这里。
<form name="loginForm" method="Post" action="LoginServlet.java">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="uname"></td></tr>
<tr><td>Password:</td>
<td><input type="password" name="pass"></td>
</tr>
<tr><td></td><td><input type="submit" value="submit" name="submit"></td></tr>
</table>
</form>
和servlet是:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DBConnection connString = new DBConnection();
String query = "";
String username= request.getParameter("uname");
String password = request.getParameter("pass");
int counter= 0;
try {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
connString.getConnection();
query="Select * from user where username='"+username+"' and password='"+password+"' ";
System.out.println(query);
Statement st = connString.getConnection().createStatement();
ResultSet rs= st.executeQuery(query);
while(rs.next()){
counter++;
}
if(counter>0){
response.sendRedirect("welcome.jsp");
}
else{
response.sendRedirect("LoginPage.jsp");
}
这是我的XML:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>WHFM</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServletPath</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:1)
<form action>
URL不得指向servlet类的类文件名。它必须指向一个可由Web浏览器公开访问的URL,正好是您需要在浏览器的地址栏中输入的URL。
您已在/LoginServletPath
的网址格式上映射了servlet,因此http://localhost:8080/WHFM/LoginServletPath
可以使用该格式,因此您需要相应地修复<form action>
网址:
<form action="LoginServletPath">
或者,如果您希望能够在任何地方移动JSP文件而不必担心相对URL,
<form action="${pageContext.request.contextPath}/LoginServletPath">
无关。我会尽快解决这个问题。
答案 1 :(得分:0)
确保您在webapp文件夹中找到所有类并映射您的'servlet'和'servlet-mapping'在您的web.xml中
喜欢:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>your.package.name.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
这篇文章可能有所帮助:Invoker Servlet