07hnust

导航

用户必须先登录,否则无法完成任何操作

登录jsp页面:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html>
<head>
<title>LoginForm form</title>
<link rel="stylesheet" type="text/css" href="format1.css"/>
</head>

<script type="text/javascript">
  function getIdPassword()
  {
  
    var patrn=/^\d{1}|\d{2}$/;
  var id = document.form.id;  
  var password = document.form.password;
   if(id.value !=""&&password.value !="")
   {
   if ( id.value.match(patrn) !=null)
   {
    return true;
   }
   else
   {
    alert("ID输入错误");
    return false;
   }
   }
   else
   {
    alert("ID或密码为空!");
    return false;
   }
   }
 </script>
<body class="bg">
 <%
  request.setCharacterEncoding("UTF-8");
  response.setCharacterEncoding("UTF-8");
 %>
 <form action="login.do" method="post" name="form"
  onsubmit="return getIdPassword()">
  <table>
   <tr>
    <td>类型:</td>
    <td><input name="usertype" type="radio" value="supermanager" />超级管理员
    </td>
    <td><input name="usertype" type="radio" value="commonmanager"
     checked />普通管理员</td>

   </tr>
   <tr>
    <td>ID:</td>
    <td colspan="2"><input name="id" type="text" />${sessionScope.warning}
    </td>
   </tr>
   <tr>
    <td>密码:</td>
    <td colspan="2"><input name="password" type="password">
    </td>
   </tr>
   <tr>
    <td><input name="submit" type="submit" value="submit">
    </td>
    <td></td>
    <td><input name="reset" type="reset" value="reset">
    </td>
   </tr>  </table>
 </form>
</body>
</html>

 FormBean源代码:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.performance.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 03-03-2012
 *
 * XDoclet definition:
 * @struts.form name="loginForm"
 */
public class LoginForm extends ActionForm
{
 private String usertype;
 private String  id;
 private String  password;
   public String getUsertype()
 {
  return usertype;
 }

 public void setUsertype(String usertype)
 {
  this.usertype = usertype;
 }

 public String getId()
 {
  return id;
 }

 public void setId(String id)
 {
  this.id = id;
 }

 public String getPassword()
 {
  return password;
 }

 public void setPassword(String password)
 {
  this.password = password;
 }

 }

用以保存状态的User实体类:

package com.performance.entity;

public class User
{
 private String usertype;
 private String id;
 private String password;
 public String getUsertype()
 {
  return usertype;
 }
 public void setUsertype(String usertype)
 {
  this.usertype = usertype;
 }
 @Override
 public String toString()
 {
  return "User [usertype=" + usertype + ", id=" + id + ", password="
    + password + "]";
 }
 public String getId()
 {
  return id;
 }
 public void setId(String id)
 {
  this.id = id;
 }
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }

}

ActionBean源代码:

package com.performance.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.performance.entity.TuserInfo;
import com.performance.entity.User;
import com.performance.operation.Operation;
import com.performance.struts.form.LoginForm;

public class LoginAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
 {
  LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method
            // stub
  String usertype = loginForm.getUsertype();
  String id = loginForm.getId();
  String password = loginForm.getPassword();
    if (usertype.equals("supermanager"))
  {
   if (id.equals("1") && password.equals("a"))
   {
    User user = new User();
    user.setId(id);
    user.setPassword(password);
    user.setUsertype(usertype);
    request.getSession().setAttribute("user", user);

    request.setAttribute("msg", id);
    System.out.println(user);
    System.out.println("-----------3-------------");
    return mapping.findForward("super");
   } else
   {
    request.getSession().setAttribute("warning", "ID或密码错!");
    return mapping.findForward("failure");
   }
  } else

  {
   User user = new User();
   user.setId(id);
   user.setPassword(password);
   user.setUsertype(usertype);
   request.getSession().setAttribute("user", user);
   System.out.println(user);

   Operation operation = new Operation();
   TuserInfo common = operation.findById(id);

   System.out.println(common);
   if (common != null
     && common.getFloginPass().trim().equals(password))
   {
    request.getSession()
      .setAttribute("msg", common.getFloginName());
    return mapping.findForward("common");
   } else
   {
    request.getSession().setAttribute("warning", "ID或密码错!");
    return mapping.findForward("failure");
   }
  }

 }
}

在用来判断是否登录的jsp测试页面:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.performance.entity.User"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />

<title>super.jsp</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="format.css" />
</head>
<body>
 <%
  User user = (User) session.getAttribute("user");
   if (user == null)
   {
 %>
 <jsp:forward page="visitor.jsp" />
 <%
  }
   if (user != null)
   {
 %>

 欢迎超级管理员 ${requestScope.msg}.
 <form action="findAllTuserInfo.do" method="post">
  <input type="submit" value="showAllTuserInfo" name="showAllTuserInfo"><br>
  <input type="button" value="addTuserInfo"
   onclick="window.open('addTuserInfo.jsp')"> <br> <input
   type="button" value="deleteTuserInfo"
   onclick="window.open('deleteTuserInfo.jsp')"> <br> <input
   type="button" value="addTuserInfo"
   onclick="window.open('deleteTuserInfo.jsp')"> <br> <input
   type="button" value="updateTuserInfo"
   onclick="window.open('updateTuserInfo.jsp')"> <br> <input
   type="button" value="nullifyTuserInfo"
   onclick="window.open('nullifyTuserInfo.jsp')"> <br> <input
   type="button" value="addTikey" onclick="window.open('addTikey.jsp')">
  <br> <input type="button" value="deleteTikey"
   onclick="window.open('deleteTikey.jsp')"><br> <input
   type="button" value="updateTikey"
   onclick="window.open('updateTikey.jsp')">

 </form>
 <%
  }
 %>
</body>

</html:html>

测试页面visitor.jsp:

<%@ page language="java" pageEncoding="UTF-8"%>

<head>

  </head>
 
  <body>
 游客!<br>
 <br>
 <a href="login.jsp">登录</a>
 <br>
  </body>
</html:html>

Ps:另外一种方法:

步骤一:登录时,必须在登录页的ActionBean中进行记录User

User user = new User();
user.setId(id);
user.setPassword(password);
user.setUsertype(usertype);
request.getSession().setAttribute("user", user);

步骤二:在前台页面提交到的事物时进行判断,即为提交到的ActionBean中判断

User user=(User)request.getSession().getAttribute("user");

if(user == null)

{

return mapping.findforward("visitor");

}else

{

.....

.....

}

 

posted on 2012-03-09 15:06  07hnust  阅读(345)  评论(0编辑  收藏  举报