首先你得保证你提交失败后能够通过请求对象跳转回提交页面
请求对象跳转语法/: request.getRequestDispatcher(“类名.jsp”).forward(request,response);
下面是放在表单里面的语句
用户名: input name=“username” id=“username” value="${param.username}">
密码 :<input name="password" id="password" value="${param.password}" >
注意这里的 value="${param.username}"
这个是EL表达式 语法 ${param.参数}
参数为你设置的 标签的name的值。
登陆代码
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>
<title>登陆页面
</head>
<body>
<h1>这是登陆页面
<form action=“dologin.jsp” id=“ff”>
<!-- 实现参数回填 (回显) -->
<!-- EL 表达式 ${param.参数} -->
用户名:<input name="username" id="username" value="${param.username}">
密码:<input name="password" id="password" value="${param.password}" ><!-- "${param.password} " 有空格 空格会一起输出 字符也是-->
<input type="submit" value="登陆" >
</form>
<a href=“Goods.jsp”>返回
</body>
</html>
测试能否登陆代码
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>
<title>Insert title here
</head>
<body>
<%
//判断从 login.jsp页面提交过来的用户名和密码是否正确
String username=request.getParameter(“username”);
String pwd=request.getParameter(“password”);
if(“yc”.equalsIgnoreCase(username)&&“123”.equalsIgnoreCase(pwd)){
//如果正确 则跳转 taobao.jsp
response.sendRedirect(“taobao.jsp”);//里面的页面可以自己创建用来测试
}else{
//使用请求对象向目标页面 推送 消息(信息 数据)
request.setAttribute(“msg”, “用户名或密码错误”);
//跳回到 login。
request.getRequestDispatcher(“login.jsp”).forward(request, response);
}
%>
</body>
</html>