JSP所请求的资源不可用 [英] JSP The requested resource is not available
问题描述
启动loginPage.jsp表单并输入用户名和密码时出现此错误:
I have this error when i started loginPage.jsp form and type a username and password :
type Status report
message /WHFM/LoginServlet.java
description The requested resource is not available.
我想念什么?我阅读了有关同一问题的文章,但我认为区分大小写是正确的.
What am i missing? I read article about same problem, but I think case sensitive is right, here.
<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>
推荐答案
<form action>
URL不得指向servlet类的类文件名.它必须指向一个Web浏览器可以公开访问的URL,与您在浏览器的地址栏中输入的URL完全相同.
The <form action>
URL must not point to the class file name of the servlet class. It must point to an URL which is publicly accessible by a webbrowser, exactly the one as you'd need to enter in browser's address bar.
您已将servlet映射到/LoginServletPath
的URL模式上,因此http://localhost:8080/WHFM/LoginServletPath
可以使用它,因此您需要相应地修复<form action>
URL:
You have mapped the servlet on an URL pattern of /LoginServletPath
, so it is available by http://localhost:8080/WHFM/LoginServletPath
, so you need to fix your <form action>
URL accordingly:
<form action="LoginServletPath">
或者,如果您希望能够在任何地方移动JSP文件而不用担心相对URL,则
or, if you prefer to be able to move your JSP file around everywhere without worrying about relative URLs,
<form action="${pageContext.request.contextPath}/LoginServletPath">
Unrelated to the concrete problem, your JDBC code is leaking resources. I'd fix that as soon as possible.
这篇关于JSP所请求的资源不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!