MVC循序渐进(struts实现的登录)(五)

类别:Java 点击:0 评论:0 推荐:
MVC循序渐进(环境篇) eclipse+tomcat(一)
MVC循序渐进(jsp model1实现的登录)(二)
MVC循序渐进(jsp model2实现的登录)(三)
MVC循序渐进(webwork实现的登录)(四)
为了以后比较分析,我把所有的实现代码放到一个web应用程序中,所以看这篇文章前请看以上系列文章。

一、安装struts

http://struts.apache.org/download.cgi   下载最新版本的jakarta-struts-1.2.4.zip解压

我们用到的jar包

lib目录下所有jar包 拷贝到WEB-INF/lib 文件夹下。

二、配置

webapp\WEB-INF\web.xml

webapp\WEB-INF\struts-config.xml


webapp\WEB-INF\web.xml
***************************************************************
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>Examples</display-name>
 <description>Examples. </description>
 <servlet>
  <servlet-name>webwork</servlet-name>
  <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>model2.LoginServlet</servlet-class>
 </servlet>
 <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config/struts</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
 <servlet-mapping>
  <servlet-name>webwork</servlet-name>
  <url-pattern>*.webwork</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
    <servlet-name>action</servlet-name>
 <url-pattern>*.struts</url-pattern>
  </servlet-mapping>
 <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/model2/LoginServlet</url-pattern>
 </servlet-mapping>
 <taglib>
  <taglib-uri>webwork</taglib-uri>
  <taglib-location>/WEB-INF/lib/webwork-2.1.7.jar</taglib-location>
 </taglib>
</web-app>
***************************************************************
webapp\WEB-INF\struts-config.xml

***************************************************************
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <data-sources />
 <form-beans>
  <form-bean name="loginForm" type="struts.LoginForm" />
 </form-beans>
 <global-exceptions />
 <global-forwards/>
 <action-mappings>
  <action path="/login"
   type="struts.LoginAction"
   name="loginForm" scope="request">
   <forward name="error" path="/login.jsp" />
   <forward name="succeed" path="/succeed.jsp" />
  </action>
 </action-mappings>
 <message-resources parameter="MessageResources" />
</struts-config>
***************************************************************

三、程序

webapp\WEB-INF\src\struts\ LoginForm.java

webapp\WEB-INF\src\struts\ LoginAction.java

webapp\struts\login.jsp

webapp\struts\ succeed.jsp

webapp\WEB-INF\src\struts\ LoginForm.java
***************************************************************
package struts;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
 private String username;
 private String password;
 public void setUsername(String username) {
  this.username = username;
 }
 public String getUsername() {
  return this.username;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getPassword() {
  return this.password;
 }
}
***************************************************************
webapp\WEB-INF\src\struts\ LoginAction.java
***************************************************************
package struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  boolean isSucceed = false;
  String msg = "";
  String username = ((LoginForm) form).getUsername();
  String password = ((LoginForm) form).getPassword();

  if (username != null) {
   System.out.println("登陆处理开始.........");
   username = username.trim();
   password = password.trim();
   if (username.equals("") && username.equals("")) {
    isSucceed = false;
    msg = "用户名或密码为空";
   } else {
    isSucceed = true;
   }
   if (isSucceed) {
    if (username.equals("zkjbeyond")
      && password.equals("zkjbeyond")) {
     isSucceed = true;
     HttpSession session = request.getSession();
     msg = "登陆成功";
     session.setAttribute("username", username);

    } else {
     isSucceed = false;
     msg = "用户名密码不正确";
    }
   }
  } else {
   isSucceed = false;
   msg = "请输入用户名密码";
  }
  System.out.println(username);
  System.out.println(msg + isSucceed);
  System.out.println("登陆处理结束.........");
  if (isSucceed) {
   return mapping.findForward("succeed");
  } else {
   request.setAttribute("msg", msg);
   return mapping.findForward("error");
  }
 }
}
***************************************************************
webapp\struts\login.jsp
***************************************************************
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<%
//request.setCharacterEncoding("GBK");
Object obj=request.getAttribute("msg");
String msg="";
if(obj!=null)
msg=obj.toString();
%>
<body bgcolor="#FFFFFF">
<form action="login.struts" method="post">
<div>
<span style="width:70;">用户名:</span>
<span><input type="text" name="username"></span>
</div>
<div>
<span style="width:70;">密 码:</span>
<span><input type="password" name="password"></span>
</div>
<div>
<span style="width:70;"></span>
<span><input type="submit"></span>
</div>
<div style="color:red;">
<%
if(!msg.equals(""))
    out.println(msg);  
%>
</div>
</form>
</body>
</html>
***************************************************************
webapp\struts\ succeed.jsp
***************************************************************
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>succeed</title>
</head>
<body bgcolor="#FFFFFF">
<%
Object username=session.getAttribute("username");
if(username!=null)
  out.println("用户名为:"+username);
else
  response.sendRedirect("login.jsp");
%>
</body>
</html>
***************************************************************

http://localhost:8080/webapp/struts/login.jsp 访问




本文地址:http://com.8s8s.com/it/it12019.htm