从jsp页面转到servlet时出现404错误 下面是相关代码,求解
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletExcepti...
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置字符集
req.setCharacterEncoding("UTF-8");
//获取参数
String username=req.getParameter("username");
String password=req.getParameter("password");
UserBiz ub=new UserBizImpl();
User user=ub.login(username, password);
if(user==null){
req.setAttribute("messge", "用户名或密码错误!");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}else{
req.setAttribute("Login", user.getUsername());
resp.sendRedirect("manage.jsp");
}
}
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/com/Login</url-pattern>
</servlet-mapping>
<form action="Login" method="post">
<table width="300" bgcolor="#00FF00" align="center" border="1">
<caption align="left">用户登录</caption>
<tr>
<td>用户姓名:<input name="username" type="text" width="20"/></td>
</tr>
<tr>
<td>用户密码:<input name="password" type="password" width="20"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="立即提交" /></td>
</tr>
</table>
</form> 展开
throws ServletException, IOException {
//设置字符集
req.setCharacterEncoding("UTF-8");
//获取参数
String username=req.getParameter("username");
String password=req.getParameter("password");
UserBiz ub=new UserBizImpl();
User user=ub.login(username, password);
if(user==null){
req.setAttribute("messge", "用户名或密码错误!");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}else{
req.setAttribute("Login", user.getUsername());
resp.sendRedirect("manage.jsp");
}
}
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/com/Login</url-pattern>
</servlet-mapping>
<form action="Login" method="post">
<table width="300" bgcolor="#00FF00" align="center" border="1">
<caption align="left">用户登录</caption>
<tr>
<td>用户姓名:<input name="username" type="text" width="20"/></td>
</tr>
<tr>
<td>用户密码:<input name="password" type="password" width="20"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="立即提交" /></td>
</tr>
</table>
</form> 展开
3个回答
展开全部
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置字符集
req.setCharacterEncoding("UTF-8");
//获取参数
String username=req.getParameter("username");
String password=req.getParameter("password");
UserBiz ub=new UserBizImpl();
User user=ub.login(username, password);
if(user==null){
req.setAttribute("messge", "用户名或密码错误!");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}else{
req.setAttribute("Login", user.getUsername());
resp.sendRedirect("manage.jsp");
}
}
// servlet中再加上这段代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/com/Login</url-pattern>
</servlet-mapping>
//acation改为这个
<form action="com/Login" method="post">
<table width="300" bgcolor="#00FF00" align="center" border="1">
<caption align="left">用户登录</caption>
<tr>
<td>用户姓名:<input name="username" type="text" width="20"/></td>
</tr>
<tr>
<td>用户密码:<input name="password" type="password" width="20"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="立即提交" /></td>
</tr>
</table>
</form>
如有疑问,可以继续联系我。
祝,学习愉快。
throws ServletException, IOException {
//设置字符集
req.setCharacterEncoding("UTF-8");
//获取参数
String username=req.getParameter("username");
String password=req.getParameter("password");
UserBiz ub=new UserBizImpl();
User user=ub.login(username, password);
if(user==null){
req.setAttribute("messge", "用户名或密码错误!");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}else{
req.setAttribute("Login", user.getUsername());
resp.sendRedirect("manage.jsp");
}
}
// servlet中再加上这段代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/com/Login</url-pattern>
</servlet-mapping>
//acation改为这个
<form action="com/Login" method="post">
<table width="300" bgcolor="#00FF00" align="center" border="1">
<caption align="left">用户登录</caption>
<tr>
<td>用户姓名:<input name="username" type="text" width="20"/></td>
</tr>
<tr>
<td>用户密码:<input name="password" type="password" width="20"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="立即提交" /></td>
</tr>
</table>
</form>
如有疑问,可以继续联系我。
祝,学习愉快。
追问
提交成功后,转换到manage.jsp时,返回的结果为null,怎么回事,manage.jsp如下
欢迎:
追答
欢迎:
注意区分大小写。。。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在JSP页面中<form action="Login" method="post">指定方法是post,但是你在servlet中的方法是doGet(),这肯定不行的,你的doPost()呢?你要不在doPost()中引用doGet()方法,要不把method="post"改成method="get"都可以!
追问
提交成功后,返回的结果为null,怎么回事
追答
肯定要为空的,你上面是req.setAttribute("Login", user.getUsername());,下面又是,应该是,这要区分大小写的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
404代码代表找不到请求的路径
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询