纯JSP实现的简单登录示例

所属分类: 网络编程 / JSP编程 阅读数: 956
收藏 0 赞 0 分享

本文实例讲述了纯JSP实现的简单登录的方法。分享给大家供大家参考,具体如下:

文件共有四个web.xml、login.jsp、logout.jsp、welcome.jsp四个文件

测试环境:Tomcat 6.0.x

假设项目名称是LoginSample,我的目录结构是这样的

...\webapps\LoginSample\WEB-INF\web.xml
...\webapps\LoginSample\login.jsp
...\webapps\LoginSample\logout.jsp
...\webapps\LoginSample\welcome.jsp

web.xml源码清单:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
  <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>
</web-app>

login.jsp源码清单:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
 <head>
  <title>JSP简单登录实例</title>
 </head>
 <body>
 <h2>请登录</h2>
 <form method="POST" >
  Login Name: <input type="text" name="Name"><br>
  Login Password: <input type="text" name="Password" ><br>
  <input type="submit" value="Send"><br>
 <form>
 <%
   if (request.getParameter("Name") != null
       && request.getParameter("Password") != null) {
     String Name = request.getParameter("Name");
     String Password = request.getParameter("Password");
     if (Name.equals("a") && Password.equals("a")) {
       session.setAttribute("Login", "OK");
       session.setAttribute("myCount", new Integer(1));
       response.sendRedirect("welcome.jsp");
     }
     else {
       %>
       登录失败:用户名或密码不正确~
       <%
     }
   }
%>
 </body>
</html>

logout.jsp源码清单:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
 <%
  session.setAttribute("Login", "");
 %>
 <body>
 <h2>你已经退出登录</h2>
 </body>
</html>

welcome.jsp源码清单:

<%@ page contentType="text/html" pageEncoding="UTF-8" import="java.util.*"%>
<html>
 <body>
 <h2>欢迎页面(测试session)</h2>
 <%
 String Login = (String)session.getAttribute("Login");
 int   nCount=0;
 if (Login != null && Login.equals("OK")) {
   Integer myCount = (Integer)session.getAttribute("myCount");
   if(myCount!=null)
   {
     nCount = myCount.intValue();
     nCount = nCount + 1;
     session.setAttribute("myCount",new Integer(nCount));
   }
   %>
   登录成功,myCount=<%=nCount%></br>
   <input type=button value="退出" onclick="javascript:location.href='logout.jsp'">
   <%
 }
 else {
%>
   <jsp:forward page="login.jsp"/>
<%
  }
  %>
  </body>
</html>

希望本文所述对大家JSP程序设计有所帮助。

更多精彩内容其他人还在看

weblogic 8.1下重新编译java类但不用重启服务器的方法

weblogic 8.1下重新编译java类但不用重启服务器的方法
收藏 0 赞 0 分享

JSP下动态INCLUDE与静态INCLUDE的区别分析

这篇文章给大家介绍了JSP下动态INCLUDE与静态INCLUDE的区别分析,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

jsp中文乱码 jsp mysql 乱码的解决方法

当使用JSP页面将中文数据添加到MySql数据库中的时候发现变为乱码,或者从mysql中读取中文的时候出现乱码,这些问题根源都是由于字符编码不一致造成的。本文介绍jsp mysql 乱码的解决方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Jsp页面实现文件上传下载类代码第1/2页

Jsp页面实现文件上传下载类代码
收藏 0 赞 0 分享

下载完成后页面不自动关闭的方法

其实就一句话js代码,window.close()
收藏 0 赞 0 分享

JBuilder2005实现重构

JBuilder2005实现重构
收藏 0 赞 0 分享

CORBA对象生命周期

CORBA对象生命周期
收藏 0 赞 0 分享

基于Java的代理设计模式

基于Java的代理设计模式
收藏 0 赞 0 分享

Java中四种XML解析技术

Java中四种XML解析技术
收藏 0 赞 0 分享

跨平台Java程序

跨平台Java程序
收藏 0 赞 0 分享
查看更多