程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 架構實例之Demo_JSP_JavaBean_Servlet,jspservletjavabean

架構實例之Demo_JSP_JavaBean_Servlet,jspservletjavabean

編輯:JAVA綜合教程

架構實例之Demo_JSP_JavaBean_Servlet,jspservletjavabean


架構實例之Demo_JSP_JavaBean_Servlet

1、開發工具和開發環境

      開發工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13

     開發環境:WIN10

2、Demo_JSP_JavaBean_Servlet實現功能

      用戶登錄、用戶注冊、退出登錄。

3、Demo_JSP_Java_Bean_Servlet使用技術

      本實例使用了JSP、JavaBean、Servlet和JDBC來實現用戶登錄、用戶注冊和退出登錄功能。系統架構圖如圖一所示:

 圖一:Demo_JSP_Java_Bean_Servlet系統架構圖

     下面請看圖二(系統中JSP、JavaBean和Servlet之間的邏輯關系圖):

  

 

圖二:系統中JSP、JavaBean和Servlet之間的邏輯關系圖

 

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

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved