**
- 项目需求
**
- 实现用户登录
- 实现用户登出
- 实现用户注册
- 功能分析:
- 实现用户注册:根据用户名和密码查询用户信息,查有则登录成功,查无则登录失败;
- 用户退出:销毁session;
- 用户注册:将用户信息录入数据库;
- 数据库设计:
- 表名:t_user;
- 用户id:uid;
- 用户名:uname;
- 用户密码:pwd;
- 性别:sex;
- 年龄:age;
- 出生日期:birthday;
- 数据库命令设计:
- 用户登录:select * from t_user where uname=? and pwd=?;
- 用户注册:insert into t_user values (default,?,?,?,?,?)
**
MVC模式:
**
- M:model, 包含service层、Dao层和实体类层;
- V:view,视图jsp页面等;
- C:controller, 控制层,servlet层等
一、用户登录的实现
- 项目环境
1)页面源码(不要只复制jsp,其他的一些也要复制进来);
2)my-sql.connector jar包,这里用的是mysql-connector-java-5.1.30;
3)jdbc操作工具类,DBUtil类放在util包里,db.properties;
2.实现步骤
1)用户点击登录发送请求到UserServlettomcat服务器收到请求后调用UserServlet中的service方法进行请求处理;并将封存了相关数据的request对象和response对象作为实参传递给service方法
2) 在UserServlet中调用业务层方法进行登录业务处理,在业务层方法中调用Dao层方法完成数据库操作
3)完成功能跳转
C层:
1)UserSerlvet
package com.facai.user;
import java.io.IOException;
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.facai.pojo.User;
import com.facai.service.UserService;
import com.facai.service.impl.UserServiceImpl;
public class UserServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
System.out.println(uname+":"+pwd);
//处理请求信息
//创建业务层
UserService us=new UserServiceImpl();
User u=us.getUserInfoService(uname,pwd);
System.out.println("用户登录查询结果为:"+u);
//响应处理结果
//重定向
//创建session对象
HttpSession session=req.getSession();
if(u!=null){
//登录成功
//重定向到main.jsp
resp.sendRedirect("/project/main.jsp");
session.setAttribute("uname",uname);
}else{
//登录失败
//将登录失败的标记添加到session中
session.setAttribute("flag", "LoginFalse");
//重定向到login.jsp
resp.sendRedirect("/project/login.jsp");
}
}
}
M层:
1)User
package com.facai.pojo;
public class User {
private int uid;
private String uname;
private String pwd;
private String sex;
private int age;
private String birthday;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public User(int uid, String uname, String pwd, String sex, int age,
String birthday) {
super();
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
this.sex = sex;
this.age = age;
this.birthday = birthday;
}
public User() {
super();
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", pwd=" + pwd
+ ", sex=" + sex + ", age=" + age + ", birthday=" + birthday
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
result = prime * result + uid;
result = prime * result + ((uname == null) ? 0 : uname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age != other.age)
return false;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (pwd == null) {
if (other.pwd != null)
return false;
} else if (!pwd.equals(other.pwd))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
if (uid != other.uid)
return false;
if (uname == null) {
if (other.uname != null)
return false;
} else if (!uname.equals(other.uname))
return false;
return true;
}
}
2)UserService接口和UserServiceImpl实现类
UserService
package com.facai.service;
import com.facai.pojo.User;
public interface UserService {
/**
* 用户登录
* @param uname
* @param pwd
* @return
*/
User getUserInfoService(String uname, String pwd);
}
UserServiceImpl
package com.facai.service.impl;
import com.facai.dao.UserDao;
import com.facai.dao.impl.UserDaoImpl;
import com.facai.pojo.User;
import com.facai.service.UserService;
public class UserServiceImpl implements UserService {
//创建Dao层对象
UserDao ud=new UserDaoImpl();
@Override
public User getUserInfoService(String uname, String pwd) {
// TODO Auto-generated method stub
return ud.getUserInfoService(uname,pwd);
}
}
3)UserDao接口和UserDaoImpl实现类
UserDao接口
package com.facai.dao;
import com.facai.pojo.User;
public interface UserDao {
/**
* 根据用户名和密码查询用户信息
*/
User getUserInfoService(String uname, String pwd);
}
UserDaoImpl实现类
package com.facai.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.facai.dao.UserDao;
import com.facai.pojo.User;
import com.facai.util.DBUtil;
public class UserDaoImpl implements UserDao{
@Override
public User getUserInfoService(String uname, String pwd) {
// 声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明变量
User u=null;
try {
//创建连接
conn=DBUtil.getConnection();
//创建SQL语句
String sql="select * from t_user where uname=? and pwd=?";
//创建SQL语句对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1,uname);
ps.setString(2,pwd);
//执行SQL命令
rs=ps.executeQuery();
//遍历
while(rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirthday(rs.getString("birthday"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源
DBUtil.closeAll(rs,ps, conn);
}
//返回结果
return u;
}
}
V层:
1)login.jsp(登录页面)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath %>>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>登录</title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
</head>
<body>
<div class="bg"></div>
<div class="container">
<div class="line bouncein">
<div class="xs6 xm4 xs3-move xm4-move">
<div style="height:150px;"></div>
<div class="media media-y margin-big-bottom">
</div>
<form action="user" method="post">
<div class="panel loginbox">
<div class="text-center margin-big padding-big-top"><h1>后台管理中心</h1></div>
<!-- 声明java代码块 -->
<%
//获取session 标记
Object obj=session.getAttribute("flag");
if(obj!=null){
%>
<div style="text-align:center;color:red">用户名或密码错误</div>
<%
}
session.invalidate();
%>
<div class="panel-body" style="padding:30px; padding-bottom:10px; padding-top:10px;">
<div class="form-group">
<div class="field field-icon-right">
<input type="text" class="input input-big" name="uname" placeholder="登录账号" data-validate="required:请填写账号" />
<span class="icon icon-user margin-small"></span>
</div>
</div>
<div class="form-group">
<div class="field field-icon-right">
<input type="password" class="input input-big" name="pwd" placeholder="登录密码" data-validate="required:请填写密码" />
<span class="icon icon-key margin-small"></span>
</div>
</div>
<div class="form-group">
<div class="field">
<input type="text" class="input input-big" name="code" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" />
<img src="images/passcode.jpg" alt="" width="100" height="32" class="passcode" style="height:43px;cursor:pointer;" onclick="this.src=this.src+'?'">
</div>
</div>
</div>
<div style="padding:30px;"><input type="submit" class="button button-block bg-main text-big input-big" value="登录"></div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
其中,下面这块为获取session标记及显示登录失败(注:运行完session需要销毁)
<!-- 声明java代码块 -->
<%
//获取session 标记
Object obj=session.getAttribute("flag");
if(obj!=null){
%>
<div style="text-align:center;color:red">用户名或密码错误</div>
<%
}
session.invalidate();
%>
2)main页面(登录成功页面)
<%@ page language="java" import="java.util.*,com.facai.pojo.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>后台管理中心</title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
</head>
<body style="background-color:#f2f9fd;">
<div class="header bg-main">
<div class="logo margin-big-left fadein-top">
<h1><img src="images/y.jpg" class="radius-circle rotate-hover" height="50" alt="" />后台管理中心</h1>
</div>
<div class="head-l" style="position:relative;left:1100px;"> <span style="font-size=15px;color:white;">当前用户:<%=((String)session.getAttribute("uname"))%></span> <a class="button button-little bg-red" href="login.html"><span class="icon-power-off"></span> 退出登录</a> </div>
</div>
<div class="leftnav">
<div class="leftnav-title"><strong><span class="icon-list"></span>菜单列表</strong></div>
<h2><span class="icon-user"></span>基本设置</h2>
<ul style="display:block">
<li><a href="info.html" target="right"><span class="icon-caret-right"></span>网站设置</a></li>
<li><a href="pass.html" target="right"><span class="icon-caret-right"></span>修改密码</a></li>
<li><a href="page.html" target="right"><span class="icon-caret-right"></span>单页管理</a></li>
<li><a href="adv.html" target="right"><span class="icon-caret-right"></span>首页轮播</a></li>
<li><a href="book.html" target="right"><span class="icon-caret-right"></span>留言管理</a></li>
<li><a href="column.html" target="right"><span class="icon-caret-right"></span>栏目管理</a></li>
</ul>
<h2><span class="icon-pencil-square-o"></span>栏目管理</h2>
<ul>
<li><a href="list.html" target="right"><span class="icon-caret-right"></span>内容管理</a></li>
<li><a href="add.html" target="right"><span class="icon-caret-right"></span>添加内容</a></li>
<li><a href="cate.html" target="right"><span class="icon-caret-right"></span>分类管理</a></li>
</ul>
</div>
<script type="text/javascript">
$(function(){
$(".leftnav h2").click(function(){
$(this).next().slideToggle(200);
$(this).toggleClass("on");
})
$(".leftnav ul li a").click(function(){
$("#a_leader_txt").text($(this).text());
$(".leftnav ul li a").removeClass("on");
$(this).addClass("on");
})
});
</script>
<ul class="bread">
<li><a href="{:U('Index/info')}" target="right" class="icon-home"> 首页</a></li>
<li><a href="##" id="a_leader_txt">网站信息</a></li>
<li><b>当前语言:</b><span style="color:red;">中文</php></span>
切换语言:<a href="##">中文</a> <a href="##">英文</a> </li>
</ul>
<div class="admin">
<iframe scrolling="auto" rameborder="0" src="info.jsp" name="right" width="100%" height="100%"></iframe>
</div>
<div style="text-align:center;">
<p>来源:<a href="http://www.mycodes.net/" target="_blank">源码之家</a></p>
</div>
</body>
</html>
下面代码块为设置登录用户名:
</div>
<div class="head-l" style="position:relative;left:1100px;"> <span style="font-size=15px;color:white;">当前用户:<%=((String)session.getAttribute("uname"))%></span> <a class="button button-little bg-red" href="login.html"><span class="icon-power-off"></span> 退出登录</a> </div>
</div>
到此用户登录功能完成。
二、用户退出登录的实现
1.创建OutServlet,作用为销毁session(注:这里返回处理结果用的是重定向,如果用请求转发依旧会创建session,最好使用重定向)
package com.facai.user;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class OutServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
//处理请求信息
//获取session,销毁session
HttpSession hs=req.getSession();
hs.invalidate();
//响应处理结果
resp.sendRedirect("/project/login.jsp");
}
}
2.修改main.jsp,添加退出确认交互功能
<%@ page language="java" import="java.util.*,com.facai.pojo.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>后台管理中心</title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<!-- 声明js代码域 -->
<script type="text/javascript">
$(function(){
//给退出登录添加事件
$("#out").click(function(){
return window.confirm("是否确定退出?");
})
})
</script>
</head>
<body style="background-color:#f2f9fd;">
<div class="header bg-main">
<div class="logo margin-big-left fadein-top">
<h1><img src="images/y.jpg" class="radius-circle rotate-hover" height="50" alt="" />后台管理中心</h1>
</div>
<div class="head-l" style="position:relative;left:1100px;"> <span style="font-size=15px;color:white;">当前用户:<%=((String)session.getAttribute("uname"))%></span> <a id="out" class="button button-little bg-red" href="out"><span class="icon-power-off"></span> 退出登录</a> </div>
</div>
<div class="leftnav">
<div class="leftnav-title"><strong><span class="icon-list"></span>菜单列表</strong></div>
<h2><span class="icon-user"></span>基本设置</h2>
<ul style="display:block">
<li><a href="info.html" target="right"><span class="icon-caret-right"></span>网站设置</a></li>
<li><a href="pass.html" target="right"><span class="icon-caret-right"></span>修改密码</a></li>
<li><a href="page.html" target="right"><span class="icon-caret-right"></span>单页管理</a></li>
<li><a href="adv.html" target="right"><span class="icon-caret-right"></span>首页轮播</a></li>
<li><a href="book.html" target="right"><span class="icon-caret-right"></span>留言管理</a></li>
<li><a href="column.html" target="right"><span class="icon-caret-right"></span>栏目管理</a></li>
</ul>
<h2><span class="icon-pencil-square-o"></span>栏目管理</h2>
<ul>
<li><a href="list.html" target="right"><span class="icon-caret-right"></span>内容管理</a></li>
<li><a href="add.html" target="right"><span class="icon-caret-right"></span>添加内容</a></li>
<li><a href="cate.html" target="right"><span class="icon-caret-right"></span>分类管理</a></li>
</ul>
</div>
<script type="text/javascript">
$(function(){
$(".leftnav h2").click(function(){
$(this).next().slideToggle(200);
$(this).toggleClass("on");
})
$(".leftnav ul li a").click(function(){
$("#a_leader_txt").text($(this).text());
$(".leftnav ul li a").removeClass("on");
$(this).addClass("on");
})
});
</script>
<ul class="bread">
<li><a href="{:U('Index/info')}" target="right" class="icon-home"> 首页</a></li>
<li><a href="##" id="a_leader_txt">网站信息</a></li>
<li><b>当前语言:</b><span style="color:red;">中文</php></span>
切换语言:<a href="##">中文</a> <a href="##">英文</a> </li>
</ul>
<div class="admin">
<iframe scrolling="auto" rameborder="0" src="info.jsp" name="right" width="100%" height="100%"></iframe>
</div>
<div style="text-align:center;">
<p>来源:<a href="http://www.mycodes.net/" target="_blank">源码之家</a></p>
</div>
</body>
</html>
添加js代码块(注:
为何不用var flag=window.confirm(“是否确定退出?”),这样点确认或者点返回都会退出登录)
<!-- 声明js代码域 -->
<script type="text/javascript">
$(function(){
//给退出登录添加事件
$("#out").click(function(){
return window.confirm("是否确定退出?");
})
})
</script>
并在退出登录处添加js 的id
<a id="out" class="button button-little bg-red" href="out"><span class="icon-power-off"></span> 退出登录</a>
三、用户功能的实现
C层:
1.创建RegServlet,实现注册请求处理。(注:这里把注册请求设为int类型,如果大于零说明注册成功,小于等于零说明注册失败)
package com.facai.user;
import java.io.IOException;
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.facai.service.UserService;
import com.facai.service.impl.UserServiceImpl;
public class RegServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("html/text;charset=utf-8");
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
String sex=req.getParameter("sex");
int age=Integer.parseInt(req.getParameter("age"));
String birthday=req.getParameter("birthday");
//处理请求信息
//处理业务层对象
UserService us=new UserServiceImpl();
//处理注册
int i=us.regUserInfoService(uname,pwd,sex,age,birthday);
//响应处理结果
//创建session对象
HttpSession hs=req.getSession();
//重定向
if(i>0){
hs.setAttribute("reg", "RegSuccess");
//重定向到登录页面
resp.sendRedirect("/project/login.jsp");
}else{
//重定向到注册页面
resp.sendRedirect("/project/reg.jsp");
}
}
}
M层
1.在UserService接口和UserServiceImpl类中添加处理注册请求的方法
1)UserService接口
package com.facai.service;
import com.facai.pojo.User;
public interface UserService {
/**
* 用户登录
* 用户注册
* @param uname
* @param pwd
* @return
*/
User getUserInfoService(String uname, String pwd);
int regUserInfoService(String uname, String pwd, String sex, int age,
String birthday);
}
2)UserServiceImpl
package com.facai.service.impl;
import com.facai.dao.UserDao;
import com.facai.dao.impl.UserDaoImpl;
import com.facai.pojo.User;
import com.facai.service.UserService;
public class UserServiceImpl implements UserService {
//创建Dao层对象
UserDao ud=new UserDaoImpl();
@Override
public User getUserInfoService(String uname, String pwd) {
// TODO Auto-generated method stub
return ud.getUserInfoService(uname,pwd);
}
//用户注册
@Override
public int regUserInfoService(String uname, String pwd, String sex,
int age, String birthday) {
//处理注册业务
return ud.regUserInfoService(uname,pwd,sex,age,birthday);
}
}
2.在UserDao接口和UserDaoImpl类中添加处理注册请求的方法
1)UserDao接口
package com.facai.dao;
import com.facai.pojo.User;
public interface UserDao {
/**
* 根据用户名和密码查询用户信息
*/
User getUserInfoService(String uname, String pwd);
/*
* 用户注册
*/
int regUserInfoService(String uname, String pwd, String sex, int age,
String birthday);
}
2)UserDaoImpl类
package com.facai.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.facai.dao.UserDao;
import com.facai.pojo.User;
import com.facai.util.DBUtil;
public class UserDaoImpl implements UserDao{
@Override
public User getUserInfoService(String uname, String pwd) {
// 声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明变量
User u=null;
try {
//创建连接
conn=DBUtil.getConnection();
//创建SQL语句
String sql="select * from t_user where uname=? and pwd=?";
//创建SQL语句对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1,uname);
ps.setString(2,pwd);
//执行SQL命令
rs=ps.executeQuery();
//遍历
while(rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirthday(rs.getString("birthday"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源
DBUtil.closeAll(rs,ps, conn);
}
//返回结果
return u;
}
//用户注册
@Override
public int regUserInfoService(String uname, String pwd, String sex,
int age, String birthday) {
// 创建SQL语句
String sql="insert into t_user values(default,?,?,?,?,?)";
return DBUtil.executeDML(sql, uname,pwd,sex,age,birthday);
}
}
V层:
1.创建注册页面reg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
<!--声明js代码域 -->
<script type="text/javascript">
$(function(){
//给男添加单击事件
$("#man").click(function(){
//将男的选择状态加上
$("#manSpan").addClass("icon-check");
//给女的span删除选择样式
$("#womanSpan").removeClass("icon-check");
})
//给女添加单击事件
$("#woman").click(function(){
//给女的span添加选择样式
$("#womanSpan").addClass("icon-check");
//将男的选择状态去掉
$("#manSpan").removeClass("icon-check");
})
})
</script>
</head>
<body>
<div class="panel admin-panel">
<div class="panel-head">
<strong><span class="icon-key"></span>用户注册</strong>
</div>
<div class="body-content">
<form method="post" class="form-x" action="reg">
<div class="form-group">
<div class="label">
<label for="sitename">用户名:</label>
</div>
<div class="field">
<input type="text" class="input w50" id="mpass" name="uname"
size="50" placeholder="请输入用户名" data-validate="required:请输入用户名" />
</div>
</div>
<div class="form-group">
<div class="label">
<label for="sitename">新密码:</label>
</div>
<div class="field">
<input type="password" class="input w50" name="pwd" size="50"
placeholder="请输入新密码"
data-validate="required:请输入新密码,length#>=5:新密码不能小于5位" />
</div>
</div>
<div class="form-group">
<div class="label">
<label for="sitename">确认新密码:</label>
</div>
<div class="field">
<input type="password" class="input w50"
size="50" placeholder="请再次输入新密码"
data-validate="required:请再次输入新密码,repeat#pwd:两次输入的密码不一致" />
</div>
</div>
<!-- 性别 -->
<div class="form-group">
<div class="label">
<label>性别:</label>
</div>
<div class="field">
<div class="button-group radio">
<label class="button active">
<span class="icon-check" id="manSpan"></span>
<input name="sex" value="1" id="man" type="radio" checked="checked">男
</label>
<label class="button active" ><span class="" id="womanSpan"></span>
<input name="sex" value="0" id="woman" type="radio">女
</label>
</div>
</div>
</div>
<!-- 年龄 -->
<div class="form-group">
<div class="label">
<label for="sitename">用户年龄:</label>
</div>
<div class="field">
<input type="text" class="input w50" id="mpass" name="age"
size="50" placeholder="请输入年龄" data-validate="required:请输入年龄" />
</div>
</div>
<!--出生日期 -->
<div class="form-group">
<div class="label">
<label for="sitename">出生日期:</label>
</div>
<div class="field">
<input type="date" class="input w50" id="mpass" name="birthday"
size="50" />
</div>
</div>
<div class="form-group">
<div class="label">
<label></label>
</div>
<div class="field">
<button class="button bg-main icon-check-square-o" type="submit">
提交</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
2.修改main.jsp,因为两个功能储存的session都是flag,所以不能当以是否是null来判断。
<!-- 声明java代码块 -->
<%
//获取session 标记
String str=(String)session.getAttribute("flag");
if("LoginFalse".equals(str)){
%>
<div style="text-align:center;color:red">用户名或密码错误</div>
<%
}else if("RegSuccess".equals(str)){%>
<div style="text-align:center;color:red">注册成功</div>
<% }
session.invalidate();
%>
四、到这里,这个页面还存在问题
问题:
我们的一个请求或者一个独立的业务逻辑都需要去找其对应的Servlet进行请求处理;
一个网站的功能很多,如果每个功能都创建单独的Servlet去处理请求,这样会造成Servlet的过多,造成资源浪费。
解决:
服务器在接收到浏览器的请求后,调用相应的Servlet,然后调用Servlet中的service方法进行请求处理。将不同功能的处理封装成service中的方法。在service中调用其对应的方法进行请求处理。
这样只需要一个Servlet。
新问题:
如何在service方法中实现根据请求动态调用对应方法呢?
解决:
使用反射。
注意:
请求中要附带执行的方法名称。
M层:
1.创建DataServlet,在这个Servlet中封装所有Servlet。
package com.facai.user;
/**
* 只创建一个Servlet,在service方法中动态调用处理方法
* 注:
* 请求中需带有要调用的方法名
*
*/
import java.io.IOException;
import java.lang.reflect.Method;
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.facai.pojo.User;
import com.facai.service.UserService;
import com.facai.service.impl.UserServiceImpl;
public class DataServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String methodName=req.getParameter("method");
System.out.println("当前请求方法为:"+methodName);
//调用方法(处理和响应请求)
try {
//根据方法名调用方法--->反射
//反射方法所在类的类对象
Class cla=this.getClass();
//反射获取要被调用的对象方法
Method m=cla.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//反射执行方法
m.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
//封装方法
//登录处理方法
public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException{
System.out.println("DataServlet.userLogin");
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
System.out.println(uname+":"+pwd);
//处理请求信息
//创建业务层
UserService us=new UserServiceImpl();
User u=us.getUserInfoService(uname,pwd);
System.out.println("用户登录查询结果为:"+u);
//响应处理结果
//重定向
//创建session对象
HttpSession session=req.getSession();
if(u!=null){
//登录成功
//重定向到main.jsp
resp.sendRedirect("/project2/main.jsp");
session.setAttribute("user",u);
}else{
//登录失败
//将登录失败的标记添加到session中
session.setAttribute("flag", "LoginFalse");
//重定向到login.jsp
resp.sendRedirect("/project2/login.jsp");
}
}
//退出处理方法
public void userOut(HttpServletRequest req, HttpServletResponse resp) throws IOException{
System.out.println("DataServlet.userOut");
//获取请求信息
//处理请求信息
//获取session,销毁session
HttpSession hs=req.getSession();
hs.invalidate();
//响应处理结果
resp.sendRedirect("/project2/login.jsp");
}
//注册处理方法
public void userReg(HttpServletRequest req, HttpServletResponse resp) throws IOException{
System.out.println("DataServlet.userReg");
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
String sex=req.getParameter("sex");
int age=Integer.parseInt(req.getParameter("age"));
String birthday=req.getParameter("birthday");
//处理请求信息
//处理业务层对象
UserService us=new UserServiceImpl();
//处理注册
int i=us.regUserInfoService(uname,pwd,sex,age,birthday);
//响应处理结果
//创建session对象
HttpSession hs=req.getSession();
//重定向
if(i>0){
hs.setAttribute("reg", "RegSuccess");
//重定向到登录页面
resp.sendRedirect("/project2/login.jsp");
}else{
//重定向到注册页面
resp.sendRedirect("/project2/reg.jsp");
}
}
}
V层:
1.login.jsp,form表单的对象改为DataServlet,并添加对应的method
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath %>>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>登录</title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
</head>
<body>
<div class="bg"></div>
<div class="container">
<div class="line bouncein">
<div class="xs6 xm4 xs3-move xm4-move">
<div style="height:150px;"></div>
<div class="media media-y margin-big-bottom">
</div>
<form action="data" method="post">
<!-- 声明请求处理方法 -->
<input type="hidden" name="method" value="userLogin"/>
<div class="panel loginbox">
<div class="text-center margin-big padding-big-top"><h1>后台管理中心</h1></div>
<!-- 声明java代码块 -->
<%
//获取session 标记
String str=(String)session.getAttribute("flag");
if(str!=null){
if("LoginFalse".equals(str)){
%>
<div style="text-align:center;color:red">用户名或密码错误</div>
<%
}else if("RegSuccess".equals(str)){%>
<div style="text-align:center;color:red">注册成功</div>
<% }
session.invalidate(); }
%>
<div class="panel-body" style="padding:30px; padding-bottom:10px; padding-top:10px;">
<div class="form-group">
<div class="field field-icon-right">
<input type="text" class="input input-big" name="uname" placeholder="登录账号" data-validate="required:请填写账号" />
<span class="icon icon-user margin-small"></span>
</div>
</div>
<div class="form-group">
<div class="field field-icon-right">
<input type="password" class="input input-big" name="pwd" placeholder="登录密码" data-validate="required:请填写密码" />
<span class="icon icon-key margin-small"></span>
</div>
</div>
<div class="form-group">
<div class="field">
<input type="text" class="input input-big" name="code" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" />
<img src="images/passcode.jpg" alt="" width="100" height="32" class="passcode" style="height:43px;cursor:pointer;" onclick="this.src=this.src+'?'">
</div>
</div>
</div>
<div style="padding:30px;"><input type="submit" class="button button-block bg-main text-big input-big" value="登录"></div>
<div style="font-size:15px;position:relative;top:-20px;left:330px"><a href="reg.jsp">注册</a></div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
修改处:
<form action="data" method="post">
<!-- 声明请求处理方法 -->
<input type="hidden" name="method" value="userLogin"/>
2.reg.jsp,form表单的对象改为DataServlet,并添加对应的method
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<script src="js/pintuer.js"></script>
<!--声明js代码域 -->
<script type="text/javascript">
$(function(){
//给男添加单击事件
$("#man").click(function(){
//将男的选择状态加上
$("#manSpan").addClass("icon-check");
//给女的span删除选择样式
$("#womanSpan").removeClass("icon-check");
})
//给女添加单击事件
$("#woman").click(function(){
//给女的span添加选择样式
$("#womanSpan").addClass("icon-check");
//将男的选择状态去掉
$("#manSpan").removeClass("icon-check");
})
})
</script>
</head>
<body>
<div class="panel admin-panel">
<div class="panel-head">
<strong><span class="icon-key"></span>用户注册</strong>
</div>
<div class="body-content">
<form method="post" class="form-x" action="data">
<!-- 声明请求处理方法 -->
<input type="hidden" name="method" value="userReg"/>
<div class="form-group">
<div class="label">
<label for="sitename">用户名:</label>
</div>
<div class="field">
<input type="text" class="input w50" id="mpass" name="uname"
size="50" placeholder="请输入用户名" data-validate="required:请输入用户名" />
</div>
</div>
<div class="form-group">
<div class="label">
<label for="sitename">新密码:</label>
</div>
<div class="field">
<input type="password" class="input w50" name="pwd" size="50"
placeholder="请输入新密码"
data-validate="required:请输入新密码,length#>=5:新密码不能小于5位" />
</div>
</div>
<div class="form-group">
<div class="label">
<label for="sitename">确认新密码:</label>
</div>
<div class="field">
<input type="password" class="input w50"
size="50" placeholder="请再次输入新密码"
data-validate="required:请再次输入新密码,repeat#pwd:两次输入的密码不一致" />
</div>
</div>
<!-- 性别 -->
<div class="form-group">
<div class="label">
<label>性别:</label>
</div>
<div class="field">
<div class="button-group radio">
<label class="button active">
<span class="icon-check" id="manSpan"></span>
<input name="sex" value="1" id="man" type="radio" checked="checked">男
</label>
<label class="button active" ><span class="" id="womanSpan"></span>
<input name="sex" value="0" id="woman" type="radio">女
</label>
</div>
</div>
</div>
<!-- 年龄 -->
<div class="form-group">
<div class="label">
<label for="sitename">用户年龄:</label>
</div>
<div class="field">
<input type="text" class="input w50" id="mpass" name="age"
size="50" placeholder="请输入年龄" data-validate="required:请输入年龄" />
</div>
</div>
<!--出生日期 -->
<div class="form-group">
<div class="label">
<label for="sitename">出生日期:</label>
</div>
<div class="field">
<input type="date" class="input w50" id="mpass" name="birthday"
size="50" />
</div>
</div>
<div class="form-group">
<div class="label">
<label></label>
</div>
<div class="field">
<button class="button bg-main icon-check-square-o" type="submit">
提交</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
修改处:
<form method="post" class="form-x" action="data">
<!-- 声明请求处理方法 -->
<input type="hidden" name="method" value="userReg"/>
3.main.jsp,因为“退出登录”对应a标签所以只需要改url就行
<%@ page language="java" import="java.util.*,com.facai.pojo.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title>后台管理中心</title>
<link rel="stylesheet" href="css/pintuer.css">
<link rel="stylesheet" href="css/admin.css">
<script src="js/jquery.js"></script>
<!-- 声明js代码域 -->
<script type="text/javascript">
$(function(){
//给退出登录添加事件
$("#out").click(function(){
return window.confirm("是否确定退出?");
})
})
</script>
</head>
<body style="background-color:#f2f9fd;">
<div class="header bg-main">
<div class="logo margin-big-left fadein-top">
<h1><img src="images/y.jpg" class="radius-circle rotate-hover" height="50" alt="" />后台管理中心</h1>
</div>
<div class="head-l" style="position:relative;left:1100px;"> <span style="font-size=15px;color:white;">当前用户:<%=((User)session.getAttribute("user")).getUname()%></span> <a id="out" class="button button-little bg-red" href="data?method=userOut"><span class="icon-power-off"></span> 退出登录</a> </div>
</div>
<div class="leftnav">
<div class="leftnav-title"><strong><span class="icon-list"></span>菜单列表</strong></div>
<h2><span class="icon-user"></span>基本设置</h2>
<ul style="display:block">
<li><a href="info.html" target="right"><span class="icon-caret-right"></span>网站设置</a></li>
<li><a href="pass.html" target="right"><span class="icon-caret-right"></span>修改密码</a></li>
<li><a href="page.html" target="right"><span class="icon-caret-right"></span>单页管理</a></li>
<li><a href="adv.html" target="right"><span class="icon-caret-right"></span>首页轮播</a></li>
<li><a href="book.html" target="right"><span class="icon-caret-right"></span>留言管理</a></li>
<li><a href="column.html" target="right"><span class="icon-caret-right"></span>栏目管理</a></li>
</ul>
<h2><span class="icon-pencil-square-o"></span>栏目管理</h2>
<ul>
<li><a href="list.html" target="right"><span class="icon-caret-right"></span>内容管理</a></li>
<li><a href="add.html" target="right"><span class="icon-caret-right"></span>添加内容</a></li>
<li><a href="cate.html" target="right"><span class="icon-caret-right"></span>分类管理</a></li>
</ul>
</div>
<script type="text/javascript">
$(function(){
$(".leftnav h2").click(function(){
$(this).next().slideToggle(200);
$(this).toggleClass("on");
})
$(".leftnav ul li a").click(function(){
$("#a_leader_txt").text($(this).text());
$(".leftnav ul li a").removeClass("on");
$(this).addClass("on");
})
});
</script>
<ul class="bread">
<li><a href="{:U('Index/info')}" target="right" class="icon-home"> 首页</a></li>
<li><a href="##" id="a_leader_txt">网站信息</a></li>
<li><b>当前语言:</b><span style="color:red;">中文</php></span>
切换语言:<a href="##">中文</a> <a href="##">英文</a> </li>
</ul>
<div class="admin">
<iframe scrolling="auto" rameborder="0" src="info.jsp" name="right" width="100%" height="100%"></iframe>
</div>
<div style="text-align:center;">
<p>来源:<a href="http://www.mycodes.net/" target="_blank">源码之家</a></p>
</div>
</body>
</html>
修改处:
<div class="head-l" style="position:relative;left:1100px;"> <span style="font-size=15px;color:white;">当前用户:<%=((User)session.getAttribute("user")).getUname()%></span> <a id="out" class="button button-little bg-red" href="data?method=userOut"><span class="icon-power-off"></span> 退出登录</a> </div>
**
五、问题2
**
问题:
现在我们用反射实现了在service方法中动态地根据请求调用响应的请求处理方法。
但在真实情况下,我们不会每个功能都去创建一个Servlet,但是也不会只有一个Servlet。
一般是一个独立的功能模块对于一个Servlet。而我们需要在每个Servlet中的service方法将反射代码都声明一边,会造成代码冗余。
解决:
向上抽取BaseServlet类;
实现:
我们自己的Servlet----》
抽取父类BaseServlet(service)
注:我们希望BaseServlet不能被访问(不能在web.xml中配置BaseServlet)
我们希望BaseServlet不能被实例化(改为抽象类)
----》HttpServlet
BaseServlet使用:
1.创建Servlet继承BaseServlet即可。
2.在自己的Servlet中不用声明service方法,只要声明处理方法即可。
注:请求需附带对应的方法名
M层:
1.创建BaseServlet,作为Servlet类的父类
package com.facai.user;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String methodName=req.getParameter("method");
//调用方法(处理和响应请求)
try {
//根据方法名调用方法--->反射
//反射方法所在类的类对象
Class cla=this.getClass();
//反射获取要被调用的对象方法
Method m=cla.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//反射执行方法
m.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.DataServlet 注释掉service方法,由父类service方法动态调用子类中的方法
package com.facai.user;
/**
* 只创建一个Servlet,在service方法中动态调用处理方法
* 注:
* 请求中需带有要调用的方法名
*
*/
import java.io.IOException;
import java.lang.reflect.Method;
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.facai.pojo.User;
import com.facai.service.UserService;
import com.facai.service.impl.UserServiceImpl;
public class DataServlet extends BaseServlet {
/*protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String methodName=req.getParameter("method");
System.out.println("当前请求方法为:"+methodName);
//调用方法(处理和响应请求)
try {
//根据方法名调用方法--->反射
//反射方法所在类的类对象
Class cla=this.getClass();
//反射获取要被调用的对象方法
Method m=cla.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//反射执行方法
m.invoke(this, req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}*/
//封装方法
//登录处理方法
public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException{
System.out.println("DataServlet.userLogin");
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
System.out.println(uname+":"+pwd);
//处理请求信息
//创建业务层
UserService us=new UserServiceImpl();
User u=us.getUserInfoService(uname,pwd);
System.out.println("用户登录查询结果为:"+u);
//响应处理结果
//重定向
//创建session对象
HttpSession session=req.getSession();
if(u!=null){
//登录成功
//重定向到main.jsp
resp.sendRedirect("/project2/main.jsp");
session.setAttribute("user",u);
}else{
//登录失败
//将登录失败的标记添加到session中
session.setAttribute("flag", "LoginFalse");
//重定向到login.jsp
resp.sendRedirect("/project2/login.jsp");
}
}
//退出处理方法
public void userOut(HttpServletRequest req, HttpServletResponse resp) throws IOException{
System.out.println("DataServlet.userOut");
//获取请求信息
//处理请求信息
//获取session,销毁session
HttpSession hs=req.getSession();
hs.invalidate();
//响应处理结果
resp.sendRedirect("/project2/login.jsp");
}
//注册处理方法
public void userReg(HttpServletRequest req, HttpServletResponse resp) throws IOException{
System.out.println("DataServlet.userReg");
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
String sex=req.getParameter("sex");
int age=Integer.parseInt(req.getParameter("age"));
String birthday=req.getParameter("birthday");
//处理请求信息
//处理业务层对象
UserService us=new UserServiceImpl();
//处理注册
int i=us.regUserInfoService(uname,pwd,sex,age,birthday);
//响应处理结果
//创建session对象
HttpSession hs=req.getSession();
//重定向
if(i>0){
hs.setAttribute("reg", "RegSuccess");
//重定向到登录页面
resp.sendRedirect("/project2/login.jsp");
}else{
//重定向到注册页面
resp.sendRedirect("/project2/reg.jsp");
}
}
}
**
六、jsp+servlet项目总结
**
总结:
1.套用模板进行页面快速构建
2.MVC的开发流程
3.Servlet+jsp+jdbc的功能开发流程
1)浏览器发起页面请求直接给jsp
2)浏览器发起功能请求给Servlet,servlet调用service进行业务处理
service调用Dao层进行数据库处理(jdbc),Dao层将处理结果返回给service
service再将结果放回给Servlet,(或者再进行请求转发或重定向给其他Servlet继续处理)请求转发或重定向给对应jsp做出页面响应
4.request和session作用域的使用
request:请求转发的数据流转的载体
session:重定向的数据流转的载体(可以解决一个用户不同请求的数据共享)
5.浏览器发起请求和服务器发起请求的方式(重点记忆)
非ajax请求
form表单提交:action数据提交地址;method数据提交方式
超链接标签:href为数据提交地址可以用?拼接请求数据,类似form表单的get请求方式
js中的window.location.href,用法同超链接
注意:
使用以上请求方式发起的请求,浏览器在接收到响应内容后,会将原有内容覆盖,显示响应结果。
6.BaseServlet的抽取和使用
反射
抽象类
缺陷
1.jsp中获取servlet流转的数据繁琐
2.在jsp页面中使用java代码块进行逻辑书写和阅读极不方便
3.现在的响应结果都是覆盖原有内容显示给用户