一、HttpServletRequest概述
我们在创建Servlet时会覆盖service()方法,或doGet()/doPost()方法,这些方法都有两个参数,一个代表请求的request和代表响应response
service方法中的request的类型是ServletRequest,而doGet()/doPost()方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子类接口,功能和方法更加强大。
二、Request的运行流程
三、通过Request获得请求信息
Http请求:
因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行,请 求头和请求体。
1、通过request获得请求行
获得客户端的请求方式:String getMethod()
获得请求的资源:
String getRequestURI()
StringBuffer getRequestURL()
String getContextPath() —web应用的名称
String getQueryString() —- get提交url地址后的参数字符串username=zhangsan&password=123
注意:request获得客户机(客户端)的一些信息
request.getRemoteAddr() — 获得访问的客户端IP地址
案例演示:
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="/Servlet4" method="get">
用户名:<input type="text" name="username"/><br />
密码:<input type="password" name="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
Servlet4:
@WebServlet(name = "Servlet4",
urlPatterns = {"/Servlet4"})
public class Servlet4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求方式
String method = request.getMethod();
System.out.println(method);
//获得请求的资源
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println("URI:"+requestURI);
System.out.println("URL:"+requestURL);
//获得WEB应用名称
String contextPath = request.getContextPath();
System.out.println("WEB应用:"+contextPath);
//地址后的参数字符串
String queryString = request.getQueryString();
System.out.println(queryString);
}
}
注意:request获得客户机(客户端)的一些信息
request.getRemoteAddr() — 获得访问的客户端IP地址
2、通过request获得请求头
主要方法有:
long getDateHeader(String name)
String getHeader(String name)
Enumeration getHeaderNames()
Enumeration getHeaders(String name)
int getIntHeader(String name)
3、通过request获得请求体
请求体中的内容是通过post提交的请求参数,格式是:
username=zhangsan&password=123&hobby=football&hobby=basketball
key ———————- value
username———- [ zhangsan ]
password —————[ 123 ]
hobby ——— [ football,basketball ]
以上面参数为例,通过一下方法获得请求参数:
String getParameter(String name)
String[] getParameterValues(String name)
Enumeration getParameterNames()
Map<String,String[]> getParameterMap()
注意:get请求方式的请求参数 上述的方法一样可以获得
解决post提交方式的乱码:request.setCharacterEncoding(“UTF-8”);
解决get提交的方式的乱码:parameter = new String(parameter.getbytes(“iso8859-1”),”utf-8”);
案例演示:
login1.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="/Servlet5" method="get">
用户名:<input type="text" name="username"/><br />
密码:<input type="password" name="password" /><br />
<input type="checkbox" name="hobby" value="zq">足球
<input type="checkbox" name="hobby" value="pq">排球
<input type="checkbox" name="hobby" value="lq">篮球
<input type="submit" value="提交" />
</form>
</body>
</html>
Servlet5:
@WebServlet(name = "Servlet5",
urlPatterns = {"/Servlet5"})
public class Servlet5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得单个表单值
String username = request.getParameter("username");
System.out.println(username);
String password = request.getParameter("password");
System.out.println(password);
//获得多个表单值
String[] hobbies = request.getParameterValues("hobby");
for(String hobby:hobbies){
System.out.println(hobby);
}
System.out.println("---------");
//获得所有的参数,参数封装到一个map中
Map<String, String[]> parameterMap = request.getParameterMap();
for(Map.Entry<String,String[]> entry:parameterMap.entrySet()){
System.out.println(entry.getKey());
for(String str:entry.getValue()){
System.out.println(str);
}
}
}
}
运行结果:
tom
123
zq
lq
~~~~~~~~~~~~~
username
tom
password
123
hobby
zq
lq
四、Request其他功能
4.1、request是一个域对象
request对象也是一个存储数据的区域对象,所以也具有如下方法:
setAttribute(String name, Object o)
getAttribute(String name)
removeAttribute(String name)
注意:request域的作用范围:一次请求中
4.2、request完成请求转发
获得请求转发器—-path是转发的地址
RequestDispatcher getRequestDispatcher(String path)
通过转发器对象转发
requestDispathcer.forward(ServletRequest request, ServletResponse response)
注意:ServletContext域与Request域的生命周期比较?
ServletContext:
创建:服务器启动
销毁:服务器关闭
域的作用范围:整个web应用
request:
创建:访问时创建request
销毁:响应结束request销毁
域的作用范围:一次请求中
注意:转发与重定向的区别?
1)重定向两次请求,转发一次请求
2)重定向地址栏的地址变化,转发地址不变
3)重新定向可以访问外部网站 转发只能访问内部资源
4)转发的性能要优于重定向
注意:客户端地址与服务器端地址的写法?
客户端地址:
是客户端去访问服务器的地址,服务器外部的地址,特点:写上web应用名称
服务器端地址:
服务器内部资源的跳转的地址,特点:不需要写web应用的名称
@WebServlet(name = "Servlet6",
urlPatterns = {"/Servlet6"})
public class Servlet6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//转发到Servlet3
RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet3");
dispatcher.forward(request,response);
}
}
五、案例:用户注册功能
前端代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>龙心商城首页</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container-fluid">
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<!-- 轮播图 -->
<div class="container-fluid">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- 轮播图的中的小点 -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- 轮播图的轮播图片 -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="img/1.jpg">
<div class="carousel-caption">
<!-- 轮播图上的文字 -->
</div>
</div>
<div class="item">
<img src="img/2.jpg">
<div class="carousel-caption">
<!-- 轮播图上的文字 -->
</div>
</div>
<div class="item">
<img src="img/3.jpg">
<div class="carousel-caption">
<!-- 轮播图上的文字 -->
</div>
</div>
</div>
<!-- 上一张 下一张按钮 -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<!-- 热门商品 -->
<div class="container-fluid">
<div class="col-md-12">
<h2>热门商品 <img src="img/title2.jpg"/></h2>
</div>
<div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;">
<img src="products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/>
</div>
<div class="col-md-10">
<div class="col-md-6" style="text-align:center;height:200px;padding:0px;">
<a href="product_info.htm">
<img src="products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;">
</a>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
</div>
</div>
<!-- 广告条 -->
<div class="container-fluid">
<img src="products/hao/ad.jpg" width="100%"/>
</div>
<!-- 最新商品 -->
<div class="container-fluid">
<div class="col-md-12">
<h2>最新商品 <img src="img/title2.jpg"/></h2>
</div>
<div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;">
<img src="products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/>
</div>
<div class="col-md-10">
<div class="col-md-6" style="text-align:center;height:200px;padding:0px;">
<a href="product_info.htm">
<img src="products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;">
</a>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</div>
</body>
</html>
后端代码:
package com.servlet;
import com.naruto.domain.User;
import com.naruto.utils.DataSourceUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;
/**
* Created by long on 2018/6/19.
*/
@WebServlet(name = "Register",
urlPatterns = {"/Register"})
public class Register extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取数据
//String username = request.getParameter("username");
//String password = request.getParameter("password");
//使用BeanUtils包进行自动映射封装
//BeanUtils工作原理:将map中的数据根据key和实体的属性的对应关系封装的
//只要key的名字与实体的属性的名字一样,就自动的封装到实体中
Map<String, String[]> parameterMap = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user,parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//现在这个位置user对象已经封装好了
//手动封装uid--uuid--随机不重复的字符串32位--java代码生成后是36位
user.setUid(UUID.randomUUID().toString());
//2、将参数传递给一个业务操作方法
try {
register(user);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void register(User user) throws SQLException {
//操作数据库
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),null,user.getBirthday(),user.getSex(),null,null);
}
}
运行结果:
点击注册按钮,到注册页面:
注册完成后,查看数据库:
解决中文乱码问题:
//设置request的编码
request.setCharacterEncoding("UTF-8");
乱码产生的原因:
页面将中文采用UTF-8编码,然后Servlet采用ISO8859-1解码
乱码解决:
将中文使用IOS8859-1编码,然后使用UTF-8解码
//get方式乱码解决(也适用于POST)
String username = request.getParameter("username");
//先用ISO8859-1编码,再使用UTF-8解码
username = new String(username.getBytes("ISO8859-1"),"UTF-8");
运行结果:
六、用户登陆失败的信息回显
注册成功之后重定向到登录界面:
@WebServlet(name = "Register",
urlPatterns = {"/Register"})
public class Register extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置request的编码--只适合POST方式
request.setCharacterEncoding("UTF-8");
Map<String, String[]> parameterMap = request.getParameterMap();
User user = new User();
try {
BeanUtils.populate(user,parameterMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//现在这个位置user对象已经封装好了
//手动封装uid--uuid--随机不重复的字符串32位--java代码生成后是36位
user.setUid(UUID.randomUUID().toString());
//2、将参数传递给一个业务操作方法
try {
register(user);
} catch (SQLException e) {
e.printStackTrace();
}
//3、认为注册成功跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login.jsp");
//request.getRequestDispatcher("/login.jsp").forward(request,response);
}
public void register(User user) throws SQLException {
//操作数据库
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getEmail(),null,user.getBirthday(),user.getSex(),null,null);
}
}
完成登录功能
前端代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />
<style>
body {
margin-top: 20px;
margin: 0 auto;
}
.carousel-inner .item img {
width: 100%;
height: 300px;
}
.container .row div {
/* position:relative;
float:left; */
}
font {
color: #666;
font-size: 22px;
font-weight: normal;
padding-right: 17px;
}
</style>
</head>
<body>
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<div class="container"
style="width: 100%; height: 460px; background: #FF2C4C url('images/loginbg.jpg') no-repeat;">
<div class="row">
<div class="col-md-7">
<!--<img src="./image/login.jpg" width="500" height="330" alt="会员登录" title="会员登录">-->
</div>
<div class="col-md-5">
<div
style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;">
<font>会员登录</font>USER LOGIN
<div><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo")%></div>
<form class="form-horizontal" action="/Login" method="post">
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="username" name="username"
placeholder="请输入用户名">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
<div class="col-sm-6">
<input type="password" class="form-control" id="inputPassword3" name="password"
placeholder="请输入密码">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputPassword3"
placeholder="请输入验证码">
</div>
<div class="col-sm-3">
<img src="./image/captcha.jhtml" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label> <input type="checkbox"> 自动登录
</label> <label> <input
type="checkbox"> 记住用户名
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" width="100" value="登录" name="submit"
style="background: url('./images/login.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>
后端代码Login:
@WebServlet(name = "Login",
urlPatterns = {"/Login"})
public class Login extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//1、获得用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//2、调用一个业务方法进行该用户查询
User login1 = null;
try {
login1 = login(username,password);
} catch (SQLException e) {
e.printStackTrace();
}
//3、通过user是否为null判断用户名和密码是否正确
if(login1!=null){
//用户名和密码正确
//登录成功 跳转到网站的首页
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else
{
//用户名或密码错误
//跳回当前login.jsp
request.setAttribute("loginInfo","用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
public User login(String username,String password) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user where username=? and password=?";
User user = runner.query(sql,new BeanHandler<User>(User.class),username,password);
return user;
}
}
运行结果:当用户名或密码错误的时候,回显信息