架构实例之Demo_JSP_JavaBean_Servlet

架构实例之Demo_JSP_JavaBean_Servlet

1、开发工具和开发环境

      开发工具: MyEclipse10JDK1.6.0_13(32)Tomcat7.032位),mysql5.7.13

     开发环境:WIN10

2、Demo_JSP_JavaBean_Servlet实现功能

      用户登录、用户注册、退出登录。

3、Demo_JSP_Java_Bean_Servlet使用技术

      本实例使用了JSPJavaBeanServletJDBC来实现用户登录、用户注册和退出登录功能。系统架构图如图一所示:

 

图一:Demo_JSP_Java_Bean_Servlet系统架构图

     下面请看图二(系统中JSPJavaBeanServlet之间的逻辑关系图):

 

 

 

图二:系统中JSPJavaBeanServlet之间的逻辑关系图

 

4、具体实现

       本篇博客实例代码是基于本人上一篇博客代码修改而来,其中修改的部分是增加了一个Servlet,并删除了JSP文件中处理逻辑层的文件,接下来主要讲解一下修改的部分——Servlet:

      首先在项目Demo_JSP_Java_Bean_Servlet中新建一个Servlet类:类名命名为UserServlet,包名命名为liu_servlet,该Servlet类主要实现用户登录、注册和退出的逻辑处理,并且调用JavaBean来操作数据库。UserServlet类的具体代码如下:

package liu_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 liu.UserBean;

public class UserServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Constructor of the object.
     */
    public UserServlet() {
        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 {

        String method = (String)request.getParameter("method");
        if(method==null) {
            PrintWriter out = response.getWriter();
            out.println("invalid request!");
        } else if(method.equals("login")) {
            Login(request, response);
        } else if(method.equals("logout")) {
            Logout(request, response);
        } else if(method.equals("register")) {
            Register(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 {
         doGet(request,response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    
    protected void Login(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // get parameters
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // check null
        if (username == null || password == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        // validate
        UserBean userBean = new UserBean();
        boolean isValid = userBean.valid(username, password);

        if (isValid) {
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            response.sendRedirect("welcome.jsp");
            return;
        } else {
            response.sendRedirect("login.jsp");
            return;
        }
    }

    protected void Logout(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        response.sendRedirect("login.jsp");
    }

    protected void Register(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get parameters
        String username = request.getParameter("username");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String email = request.getParameter("email");

        // check null
        if (username == null || password1 == null || password2 == null || !password1.equals(password2)) {
            response.sendRedirect("register.jsp");
            return;
        }

        // validate
        UserBean userBean = new UserBean();
        boolean isExist = userBean.isExist(username);
        if(!isExist) {
            userBean.add(username, password1, email);
            response.sendRedirect("login.jsp");
            return;
        } else {
            response.sendRedirect("register.jsp");
            return;
        }
    }

    
    public void init() throws ServletException {
        // Put your code here
    }

}

    接下来,查看Demo_JSP_Java_Bean_Servlet项目下web.xml文件,其中我的web.xml文件代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>UserServlet</servlet-name>
    <servlet-class>liu_servlet.UserServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UserServlet</servlet-name>
    <url-pattern>/UserServlet</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

    注意:<url-pattern>/UserServlet</url-pattern>这一句我是打开后修改的,未修改之前的代码应该是:<url-pattern>/servlet/UserServlet</url-pattern>。如果不把/servlet去掉,到时进行逻辑处理跳转界面时,就会出现在URL中多出了.../servlet/welcome.jsp,从而报Tomcat中没有welcome.jsp编译文件的错误。

   其他部分代码以及结果展示请查看本人上一篇博客哦:http://www.cnblogs.com/liuzhen1995/p/5700789.html

附:Demo_JSP_Java_Bean_Servlet项目源码文件百度云下载链接:http://pan.baidu.com/s/1geEOyht 密码:zhu4

 

 

posted @ 2016-07-24 16:34  舞动的心  阅读(917)  评论(0编辑  收藏  举报