网站用户登陆验证:Servlet+JSP VS Struts书剑恩仇录

? 什么是Struts框架
–从不一样的角度看待Struts框架
–Struts框架的优势

? 下载安装Struts框架
–下载配置Struts框架
–测试Struts框架
–安装Struts应用程序
–访问Struts文档

? Struts 中经常使用组件
–ActionServlet
–Action
–ActionForm
–ActionMapping
–ActionForward

? 实例
–以登陆为例,比较servlet+jsp和Struts的区别和联系
-----------------------------START-----------------------------------
什么是Struts框架(从不一样的角度看待Struts框架)
? 一个MVC设计模式框架?
–Struts为使用Servlet和JSP来开发Web应用程序提供了一个统一的框架
? 一个工具集合?
–Struts 提供了一系列工具集合,来处理Web开发中的常见任务
? 一个自定义标签工具集合?
–Struts提供了一系列标签,包括html、表单、bean、条件判断等
?仁者见仁,智者见智
–可是Struts框架,最核心的称呼还应该是MVC框架
--------------------------NEXT----------------------------------------
Struts 框架的优势
? 基于配置文件的松耦合
–在Struts框架中的一般类都被配置在一个配置文件当中(如:Action、Formbean、ActionForward等),和写在Java程序里的硬代码相比,耦合性下降了不少,在Servlet当中通常页面的跳转被写在Java程序当中,例如:request.getRequestDispatcher(“somepage”).forward(request,resp
onse);

? Formbean的强大功能
–在传统的Servlet+JSP当中,验证信息的从新显示必须有程序员来设置, 可是在Struts当中Formbean解决了这个问题。
? 集中的验证
–Struts提供的标签
? 基本html标签
? form表单标签
? bean标签
? 逻辑标签

? Struts的其余优势
–对国际化的支持、声明式异常处理等。
----------------------------NEXT-----------------------------------
下载配置Struts框架
? Struts 框架的下载站点
http://jakarta.apache.org/site/binindex.cgi
http://jakarta.apache.org/struts
? 解压下载包,了解Struts的目录结构
–Lib:jar文件
–Webapps:web应用程序
安装Struts应用程序
? 安装struts-blank.war
–将该压缩包拷贝到tomcat的webapps目录下,从新启动tomcat,访问该工程:
http://localhost:8080/struts-blanck
p_w_picpath
? 安装struts-documentation.war
–将该压缩包拷贝到tomcat的webapps目录下,从新启动tomcat,访问该工程:
http://localhost:8080/struts-documentation
p_w_picpath
p_w_picpath
p_w_picpath
------------------------NEXT--------------------------------
Struts 中的组件介绍
? ActionServlet
–Struts中的大控制器
–Struts框架的入口
–是对Servlet的封装,被配置在web.xml配置文件当中

? Action
–小控制器,处理具体的业务逻辑用例
? ActionForm
–和表单对应的一个特殊的JavaBean,是一个“邮递员”在控制器和页面之间传递数据,也提供了一个集中的验证方法
? ActionMapping
–从Struts配置文件中读取配置信息
? ActionForward
–页面之间的跳转
------------------------------NEXT--------------------------------------
Struts 实例(以用户登陆为例)
? Servlet+JSP 版本的登陆

–为了比较servlet+jsp和Struts的区别与联系,咱们首先看一下servlet+jsp如何建立用户登陆
login.jsp
p_w_picpath
successfull.jsp
p_w_picpath
failure.jsp
p_w_picpath
LoginServlet.java
p_w_picpath
测试:
redking账号登陆
p_w_picpath
登陆成功!
p_w_picpath
非redking账号登陆
p_w_picpath
登陆失败
p_w_picpath
-------------------------------------NEXT--------------------------------
下面经过数据库查询来验证用户登陆哈~
DB
p_w_picpath
UserDao.java
p_w_picpath
UserDaoImpl.java
  p_w_picpath
LoginServlet.java
package com.redking.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    

import javax.servlet.ServletException;    
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    
import javax.servlet.http.HttpSession;    

import com.redking.dao.UserDao;    
import com.redking.dao.impl.UserDaoImpl;    
import com.redking.vo.User;    

public class LoginServlet extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public LoginServlet() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                doPost(request,response);    
        }    

         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                String username = request.getParameter( "username");    
                String password = request.getParameter( "password");    
                 /*    
                if(username!=null&&username.equals("redking")){    
                        request.setAttribute("username", username);    
                        request.getRequestDispatcher("/pages/successfull.jsp").forward(request, response);    
                }else{    
                        request.getRequestDispatcher("/pages/failure.jsp").forward(request, response);    
                }    
                */
    
                UserDao dao = new UserDaoImpl();    
                User u = dao.login(username, password);    
                HttpSession session = request.getSession();    
                 if(u!= null){    
                        session.setAttribute( "user", u);    
                        request.getRequestDispatcher( "/pages/successfull.jsp").forward(request, response);    
                } else{    
                        request.getRequestDispatcher( "/pages/failure.jsp").forward(request, response);    
                }    
        }    

         /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occurs    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    

}
UserDaoImplTest.java
p_w_picpath
ConnectionUtil.java
p_w_picpath
SQLConstants.java
package com.redking.util;    

public interface SQLConstants {    
         public static final String USER_LOGIN_SQL = " select id,username,password from UserTbl " + " where username= ? and password = ? ";    
}
User.java
p_w_picpath
DBConfig.properties
p_w_picpath
login.jsp
p_w_picpath
successfull.jsp
p_w_picpath
failure.jsp
p_w_picpath
测试:
p_w_picpath
登陆成功
p_w_picpath
测试数据库中没有的用户登陆试试
p_w_picpath
测试结果
p_w_picpath
如今咱们将51cto账号加入DB再测试
p_w_picpath
51cto账户从新登陆
p_w_picpath
测试结果
p_w_picpath
--------------------------------NEXT-------------------------------------
? 使用Struts来开发用户登陆
–将Struts添加到上述存在的工程,看一下Struts是怎么样运行的。
加载Struts框架所需JAR包,加载到工程的LIB目录中
p_w_picpath
p_w_picpath
p_w_picpath
导入ActionServlet配置文件
p_w_picpath
p_w_picpath
松耦合配置文件Struts-config.xml
p_w_picpath
p_w_picpath
p_w_picpath
p_w_picpath
p_w_picpath
生成最基本的配置模板
p_w_picpath
LoginAction.java
p_w_picpath
login.jsp
p_w_picpath
web.xml
p_w_picpath
测试:
p_w_picpath
登陆成功
p_w_picpath
再次测试错误密码
p_w_picpath
登陆失败
p_w_picpath
---------------------------------END-----------------------------------
相关文章
相关标签/搜索