已有一个login.jsp作为登录页面,现建立doLogin.jsp要实现用户通过login页面登录,用户提交登录名和密码给这个login.jsp页面,在此JSP页面中获取用户登录的用户名和密码,并获取web.xml中保存的正确的用户名和密码进行验证,用application对象获取初始的用户名和密码 ;如果用户名/密码正确,在session中保存用户名,作为成功登录的记录,并跳转到home页面,反之,跳回到login页面。
login.jsp中的表单已经设置好了,但是在login.jsp页面中不管输入什么用户名和密码(即使与web.xml匹配)都不会跳转到home页面,只会跳转到login页面,而且session并没有被建立,这是为什么,哪里出错了
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录验证</title>
</head>
<body>
//获取输入的用户名和密码
<%
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
//System.out.println("用户名:"+name);
//System.out.println("密码:"+pwd);
%>
<%
//获取web.xml中的用户名和密码
String name2 = application.getInitParameter("userName");
String pswd2 =application.getInitParameter("pwd");
//System.out.println("存储的用户名:"+name2);
//System.out.println("存储的密码:"+pswd2);
//判断
if(name2.equals(name)&&pswd2.equals(pwd)){
//session记录登录成功
request.getSession().setAttribute("loginUser",name);
%>
<%--跳转--%>
<jsp:forward page="home.jsp"></jsp:forward>
<%
}else{
request.getRequestDispatcher("login.jsp").forward(request,response);
}
%>
</body>
</html>
请帮忙改正,谢谢