当前位置: 代码网 > it编程>编程语言>Java > JSP登录中Session的用法实例详解

JSP登录中Session的用法实例详解

2024年05月18日 Java 我要评论
本文实例讲述了jsp登录中session的用法。分享给大家供大家参考,具体如下:登录页面<%@ page language="java" contenttype="text/html; char

本文实例讲述了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程序设计有所帮助。

(0)

相关文章:

  • JSP出现中文乱码问题解决方法详解

    在介绍方法之前我们首先应该清楚具体的问题有哪些,笔者在本博客当中论述的jsp中文乱码问题有如下几个方面:页面乱码、参数乱码、表单乱码、源文件乱码。下面来逐一解决其中的乱码问题。一、…

    2024年05月18日 编程语言
  • jsp 实现的简易mvc模式示例

    jsp 实现的简易mvc模式示例

    本文实例讲述了jsp 实现的简易mvc模式。分享给大家供大家参考,具体如下:jsp : java servlet pagemvc全名是model view co... [阅读全文]
  • 使用JSP实现简单的用户登录注册页面示例代码解析

    实验要求: 将实验2中的系统用户登录和注册页面改为jsp页面,并部署自己的web应用于tomcat服务器中具体要求: 完成登录jsp页面设计和注册页面设计在登录页面表单中使用req…

    2024年05月18日 编程语言
  • jsp filter 过滤器功能与简单用法示例

    jsp filter 过滤器功能与简单用法示例

    本文实例讲述了jsp filter 过滤器功能与简单用法。分享给大家供大家参考,具体如下:过滤器的作用是什么?过滤器可以动态地拦截请求和响应,以变换或使... [阅读全文]
  • JSP实现分页效果

    本文实例为大家分享了jsp实现分页的具体代码,供大家参考,具体内容如下咱们在浏览网页的时候,当一个页面的数据不足以展示完全所有的内容,一般都涉及到分页,下一页的功能该怎么实现呢?首…

    2024年05月18日 编程语言
  • JSP+Servlet实现文件上传到服务器功能

    JSP+Servlet实现文件上传到服务器功能

    本文实例为大家分享了jsp+servlet实现文件上传到服务器功能的具体代码,供大家参考,具体内容如下项目目录结构大致如下:正如我在上图红线画的三个东西:dao... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com