架构实例之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

 

架构实例之Demo_JSP_JavaBean_Servlet的更多相关文章

  1. 架构实例之SpringTest

    架构实例之SpringTest 1.开发工具和开发环境       开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境 ...

  2. 架构实例之Demo_JSP_JavaBean

    架构实例之Demo_JSP_JavaBean 1.开发工具和开发环境      开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.1 ...

  3. 架构实例之Demo_JSP

    架构实例之Demo_JSP 1.开发工具和开发环境       开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境:W ...

  4. 【DDD】领域驱动设计实践 —— 架构风格及架构实例

    概述 DDD为复杂软件的设计提供了指导思想,其将易发生变化的业务核心域放置在限定上下文中,在确保核心域一致性和内聚性的基础上,DDD可以被多种语言和多种技术框架实现,具体的框架实现需要根据实际的业务场 ...

  5. JavaWeb学习之三层架构实例(三)

    引言 通过上一篇博客JavaWeb学习之三层架构实例(二)我们基本上已经实现了对学生信息列表的增删改查操作(UI除外),但是不难看出,代码冗余度太高了,尤其是StudentDao这个类,其中的增删改查 ...

  6. JavaWeb学习之三层架构实例(二)

    引言 这个实例是上一个实例JavaWeb学习 三层架构实例(一)的加强版,实现的是在前端对数据库中student表的 增.删.改.查 操作.关于三层组成云云,这里就不再叙述. 实例 效果图 先来看一下 ...

  7. linux驱动由浅入深系列:高通sensor架构实例分析之二(驱动代码结构)【转】

    本文转载自:https://blog.csdn.net/radianceblau/article/details/73498303 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分 ...

  8. linux驱动由浅入深系列:高通sensor架构实例分析之三(adsp上报数据详解、校准流程详解)【转】

    本文转载自:https://blog.csdn.net/radianceblau/article/details/76180915 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分 ...

  9. java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)

    1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...

随机推荐

  1. 使用css3新属性clip-path制作小图标

    一般一个网页上面,或多或少都会用到一些小图标,展示这些小图标的方法有很多种.最简单的做法就是将UI图上面的每个小图标都保存为图片,一个小图标就一张图片.但这也是比较笨的方法,因为浏览器同一时间最多加载 ...

  2. 基于MVC4+EasyUI的Web开发框架经验总结(16)--使用云打印控件C-Lodop打印页面或套打报关运单信息

    在最新的MVC4+EasyUI的Web开发框架里面,我整合了关于网购运单处理的一个模块,其中整合了客户导单.运单合并.到货扫描.扣仓.出仓.查询等各个模块的操作,里面涉及到一些运单套打的操作,不过由于 ...

  3. python处理空格脚本

    博客园上传代码时拷贝vs里面的代码不能直接粘贴,否则空格会不符合要求 去掉空格代码 # -*- coding: utf-8 -*- '''打开delSpace.txt文本并删除每行开头的八个空格''' ...

  4. PHP5各个版本的新功能和新特性总结

    因为 PHP 那“集百家之长”的蛋疼语法,加上社区氛围不好,很多人对新版本,新特征并无兴趣.本文将会介绍自 PHP5.2 起,直至 PHP5.6 中增加的新特征 本文目录:PHP5.2 以前:auto ...

  5. 创业公司招php商城开发者

    众筹  电商 已经融资100W美元 职位要求1.对PHP编程熟悉程度以上,有电商相关开发经验优先:2.熟悉lnmp相关配套搭建运维,开发;熟悉linux 使用3.对数据结构和算法设计有较深刻的理解:4 ...

  6. [调整] Firemonkey iOS 原生 Edit 透明框, 改变框色

    说明:iOS 原生 Edit 透明框 适用:Berlin Firemonkey 方法:在 StyleLookup 输入 transparentedit 效果: 如果有图片 Image 在这二个 Edi ...

  7. python计算器

    思路:优先级处理思路一:正则表达式+递归(计算时间慢)思路二:堆栈的方式队列:先进先出电梯-->队列上电梯(入队123):第三个人3,第二个人2,第一个人1>>> li = [ ...

  8. CSS:CSS使用Tips

    Css是前端开发中效果展现的主要部分之一,良好的Css书写习惯可以为实际的项目开发提高效率,也可以为实现良好的团队合作提供保证. 一般新手在使用Css的时候经常会犯一些错误,出现一些不经意的漏洞,如果 ...

  9. 配置文件(App.config文件)

    1. 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是 co ...

  10. MVC基础

    一.简介 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方 ...