jsp登录以及注销功能的前后端代码怎么写以及session用法
Admin 2023-04-12 群英技术资讯 841 次浏览
在日常操作或是项目的实际应用中,有不少朋友对于“jsp登录以及注销功能的前后端代码怎么写以及session用法”的问题会存在疑惑,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。本文实例讲述了JSP登录中Session的用法。分享给大家供大家参考,具体如下:
登录页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <div style="float:left;margin-top:100px;margin-left:200px;width:400px;height:300px;background:gray;"> <form action="IndexServlet" method="post"> <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px"> <div style="margin-left:70px;float:left;line-height:30px">账号:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="user"/> </div> <div style="float:left;width:400px;height:30px;background:gray;margin-top:50px"> <div style="margin-left:70px;float:left;line-height:30px">密码:</div><input style="disply:block;float:left;width:200px;height:30px;border:none;" type="text" name="password"/> </div> <div style="float:left;margin-top:50px;width:400px;height:30px;background:gray;"> <input style="float:left;width:60px;height:30px;margin-left:170px;border:none;" type="submit" name="ok" value="登录"/> </div> </form> </div> </body> </html>
检测账号密码以及设置session的IndexServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class IndexServlet
*/
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IndexServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String user = request.getParameter("user");
String password = request.getParameter("password");
String path = request.getContextPath();
HttpSession session=request.getSession();
if ("1".equals(user) && "1".equals(password)) {
session.setAttribute("name", user);
response.sendRedirect(path + "/success.jsp");
}else{
response.sendRedirect(path + "/Index.jsp");
}
}
}
成功登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
%>
<%
Object name = session.getAttribute("name");
if(name==null){
response.sendRedirect(path+"/Index.jsp");
}
%>
<html>
<head>
<title>成功页面</title>
</head>
<body>
恭喜你,骚年,<%=session.getAttribute("name") %>,成功登陆了!
<a href="out.jsp" rel="external nofollow" >注销</a>
</body>
</html>
注销功能的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String path = request.getContextPath();
%>
<%
session.removeAttribute("name");
response.sendRedirect(path+"/Index.jsp");
%>
</body>
</html>
到此,关于“jsp登录以及注销功能的前后端代码怎么写以及session用法”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。如果想要学习更多的相关知识,欢迎关注群英网络,小编每天都会给大家分享实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了SSM框架JSP使用Layui实现layer弹出层效果,文章通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要为大家详细介绍了JSP使用过滤器防止Xss漏洞,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍是动态实现web网页登陆和注册功能的示例代码,文中代码讲解详细,对我们学习JSP有一定的帮助,感兴趣的小伙伴可以跟随小编一起学习一下
这篇文章主要为大家详细介绍了JSP实现页面右下角消息弹框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了JSP实现带查询条件的通用分页组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008