不知道The requested resource (/Login/LoginServlet) is not available.
这是怎么回事!我是用MyEclipse来编的一个简单的登录的小程序,
package com.tLogin.model;
public class javaBean {
private String UserName;
private String UserPass;
public String getUsername()
{
return UserName;
}
public void setUsername (String username)
{
this.UserName = username;
}
public String getUserpass()
{
return UserPass;
}
public void setUserpass(String userpass)
{
this.UserPass = userpass;
}
public boolean validate(String username,String userpass)
{
if(username.equals("zhangsan") && userpass.equals("123456"))
return true;
else
return false;
}
}
//=========================
package com.tLogin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import com.tLogin.model.javaBean;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginServlet extends HttpServlet {
public loginServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userid = request.getParameter("userid");
String userpass = request.getParameter("userpass");
javaBean user = new javaBean();
boolean b = user.validate(userid, userpass);
String forward;
if(b)
{
HttpSession session = request.getSession(true);
session.setAttribute("userid",userid);
forward = "success.jsp";
}
else
{
forward = "failure.jsp";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException {
}
}
//================
<body>
<form method = " post " action = "/com/servlet/loginServlet" >
用户名: <input type = "text" name = “username”> <br>
密 码: <input type = "text" name = “password”> <br>
<input type = "submit" value = "提交">
</form>
<url-pattern>/com/servlet/loginServlet</url-pattern>
那么你的action中应该这么写:action = "com/servlet/loginServlet"
你servlet的url不要写复杂了,不然很容易在路径上出错的追问
这种方法我尝试过的了,MyEclipse自动生成的路径是servlet/loginServlet,但是我用loginServlet或者servlet/loginServlet都不行,最后我就把xml文档里面的路径修改成/com/servlet/loginServlet,但是还是不行!
追答路径不对,是报404错误
一点要注意,在action的url比里的少了前边的“/”
如果你的jsp文件并不和最初生成的index.jsp在同一个文件夹下的话,比如你新建了一个文件夹存放jsp文件,此时action="../loginServlet"。
这是生成的xml文档的内容
loginServlet
/servlet/loginServlet
那么我写action = "servlet/loginServlet"或者action = "loginServlet"应该都是可以的啊!但是它总是会出现The requested resource (/Login/LoginServlet) is not available这个错误!它是不是没有成功的跳到servlet执行servlet文件里面的代码呢?jsp 文件都是在同一个文件夹里。
你的web.xml里应该是这样的:
loginServlet
com.tLogin.servlet.loginServlet
loginServlet
/servlet/loginServlet
然后,如果jsp在WebRoot下的话,action应该这样写:action="servlet/loginServlet"
你看你出现的错误:The requested resource (/Login/LoginServlet) is not available,你找的路径是当前目录下的"Login/LoginServlet”,这个前边的"Login“是怎么得来的呢?