用jsp实现带 Cookie Session的登陆页面

博客介绍了使用Servlet实现注册登录功能的步骤,包括新建注册表单页面、处理表单参数的Servlet、登陆成功页面,还提及若要保存用户名可使用cookie。同时强调了注意细节,如设置编码、重定向和转发的区别、对session域的判断等,最后给出相关代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现步骤及注意事项:

  1. 新建一个注册 表单页面 用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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值