架构实例之Demo_JSP_JavaBean_Servlet
架构实例之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的更多相关文章
- 架构实例之SpringTest
架构实例之SpringTest 1.开发工具和开发环境 开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境 ...
- 架构实例之Demo_JSP_JavaBean
架构实例之Demo_JSP_JavaBean 1.开发工具和开发环境 开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.1 ...
- 架构实例之Demo_JSP
架构实例之Demo_JSP 1.开发工具和开发环境 开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境:W ...
- 【DDD】领域驱动设计实践 —— 架构风格及架构实例
概述 DDD为复杂软件的设计提供了指导思想,其将易发生变化的业务核心域放置在限定上下文中,在确保核心域一致性和内聚性的基础上,DDD可以被多种语言和多种技术框架实现,具体的框架实现需要根据实际的业务场 ...
- JavaWeb学习之三层架构实例(三)
引言 通过上一篇博客JavaWeb学习之三层架构实例(二)我们基本上已经实现了对学生信息列表的增删改查操作(UI除外),但是不难看出,代码冗余度太高了,尤其是StudentDao这个类,其中的增删改查 ...
- JavaWeb学习之三层架构实例(二)
引言 这个实例是上一个实例JavaWeb学习 三层架构实例(一)的加强版,实现的是在前端对数据库中student表的 增.删.改.查 操作.关于三层组成云云,这里就不再叙述. 实例 效果图 先来看一下 ...
- linux驱动由浅入深系列:高通sensor架构实例分析之二(驱动代码结构)【转】
本文转载自:https://blog.csdn.net/radianceblau/article/details/73498303 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分 ...
- linux驱动由浅入深系列:高通sensor架构实例分析之三(adsp上报数据详解、校准流程详解)【转】
本文转载自:https://blog.csdn.net/radianceblau/article/details/76180915 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分 ...
- java:Session(概述,三层架构实例(实现接口封装JDBC),Session实现简单购物车实例)
1.Session概述: Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时,存 ...
随机推荐
- 【T-SQL】分布抽取部分数据
好吧,我确实不知道该怎么起这个标题,整了一个“分布”,感觉还有点高档,其实没啥技术含量,看完你就知道了.情况是这样,刚刚接到一个临时任务,需要让几个营业点的销售数据[变]少一点,就是在ERP的相关报表 ...
- C# 发送Http请求 - WebClient类
WebClient位于System.Net命名空间下,通过这个类可以方便的创建Http请求并获取返回内容. 一.用法1 - DownloadData string uri = "http:/ ...
- iOS开发之CocoaPods的安装与使用
前言部分 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods) 可以用来方便的统一管理这些第三方库. 一.安装 由 ...
- 浅谈web网站架构演变过程
前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变. 该系统具备的功能: 用户模块:用户注册和管理 商品模块:商品展示和管理 交易模块:创建交易和管理 阶 ...
- ASP.NET MVC搭建项目后台UI框架—4、tab多页签支持
目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...
- UML简单例子
平时最常用到的UML图包括:用例图.类图.序列图.状态图. 用例图 主要是描述系统具有的一个功能单元.通常包含角色和用例.角色通常表示为一个系统用户,用例通常表示为系统具有的一个功能.通过用例图我们可 ...
- VS 快捷键
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...
- css中vertical-align垂直居中的认识
目标大纲 1.vertical-align为何不起作用?? vertical-align只钟情于“inline-block内联块级元素/inline元素” vertical-align属性 text- ...
- NSString 的常用操作
NSString *testStr01=@"HelloWord"; NSString *testStr02=[testStr01 substringToIndex:];//取头(从 ...
- JavaScript异步编程(2)- 先驱者:jsDeferred
JavaScript当前有众多实现异步编程的方式,最为耀眼的就是ECMAScript 6规范中的Promise对象,它来自于CommonJS小组的努力:Promise/A+规范. 研究javascri ...