实现步骤及注意事项:
- 新建一个注册 表单页面 用post方法提交到一个Servlet中
2.新建一个Servlet 用来处理表单提交过来的参数,并判断用户名和密码是否正确
–若正确 保存用户名密码到session域 重定向到成功页面
–若失败 转发到表单页面并显示错误信息
3.新建一个登陆成功页面
3.1获取session中的值,若值为空,转发到表单页面并显示错误信息
3.2 若值不为空 获取session域中的值,写到页面上(欢迎XXX登陆)
若要下一次浏览器中保存用户名
1.可以在登陆成功后,把用户名信息用cookie保存,并设置存活时间。由服务器端发到浏览器进行保存
2.在表单页面获取cookie的值,若值存在,就显示到用户名输入框中 .
注意细节
- 注意在获取表单信息时设置编码utf-8
- 重定向需要带项目名,而转发不带项目名
- 在登陆成功页面记得做session域的判断 不为空才显示信息 否则转发到表单页面输出错误信息
Servlet代码
public class LoginSevlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String name=request.getParameter("username");
String password=request.getParameter("password");
if(name.equals("pengzihao"))
{
request.getSession().setAttribute("username", name);
Cookie cookie=new Cookie("uname",name);
cookie.setMaxAge(60*60*24);
response.addCookie(cookie);
response.sendRedirect("/webdemo1/logintest/succ1.jsp");
}
else
{
request.setAttribute("msg", "你的密码或用户名错了");
request.getRequestDispatcher("/logintest/form.jsp").forward(request, response);
}
}
}
登陆表单代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'form.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String mes="";
String message=(String)request.getAttribute("msg");
if(message!=null)
{
mes=message;
}
String uname="";
Cookie[] cookies=request.getCookies();
if(cookies!=null)
{
for(Cookie c: cookies)
{
if("uname".equals(c.getName()))
{
uname=c.getValue();
}
}
}
%>
<h1 ><font> 登陆页面</font></h1>
<font color="red"><%=mes %></font>
<form action="/webdemo1/LoginSevlet" method="post">
用户名<input type="text" name="username" value="<%=uname%>"><br/>
密码<input type="password" name="password"/><br/>
<input type="submit" name="登陆"/>
</form>
</body>
</html>
登陆成功后表单代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'succ1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String username=(String)session.getAttribute("username");
if(username==null)
{
request.setAttribute("msg", "请您登陆后在进行操作");
request.getRequestDispatcher("/logintest/form.jsp").forward(request, response);
return;
}
%>
<h1><font color="red">欢迎<%=username%>登陆</font></h1>
</body>
</html>