一.Get请求和Post请求
Get请求:1.将请求参数追加在URL后面,不安全
2.传输的数据大小有限制
3.没有请求体
Post请求:1.请求参数存储在请求体中,安全
2.传输的数据大小无限制
表单提交默认为Get请求方式
二.常见的HTTP响应状态码
200 :请求成功。
302 :请求重定向。
304 :请求资源没有改变,访问本地缓存。
404 :请求资源不存在。
500 :服务器内部错误。
三.servlet完成一个用户登录的案例
1.数据表准备(仅用于测试数据)
CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`upassword` varchar(20) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert into `user`(`uid`,`username`,`upassword`) values (1,'tom','123'),(2,'张三','456');
2.数据表对应的User类
public class User {
private int uid;
private String username;
private String upassword;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", upassword='" + upassword + '\'' +
'}';
}
}
3.C3p0工具类
public class C3p0Utils {
static DataSource dataSource=null;
static {
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close(Connection connection,
Statement statement, ResultSet resultSet){
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4.数据库查询类
public class UserDao {
public User loginUser(Connection conn, String username, String upassword) throws SQLException {
QueryRunner qr = new QueryRunner();
String sql="select * from `user` where username=? and upassword=? limit 1";
Object[] params={username,upassword};
User user= qr.query(conn, sql, new BeanHandler<User>(User.class), params);
return user;
}
}
5.登录案例逻辑处理
public class UserService {
public boolean isLoginUser(String username,String upassword){
Connection conn=null;
UserDao userDao = new UserDao();
User user=null;
try {
conn=C3p0Utils.getConnection();
user = userDao.loginUser(conn, username, upassword);
System.out.println(user);
} catch (SQLException e) {
e.printStackTrace();
}finally {
C3p0Utils.close(conn,null,null);
}
if (user==null){
return false;
}else {
return true;
}
}
}
6.与页面交互层
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String upassword = request.getParameter("password");
System.out.println(username+" "+upassword );
UserService userService = new UserService();
boolean loginUser = userService.isLoginUser(username, upassword);
if (loginUser){
response.sendRedirect("success.jsp");
}else {
response.sendRedirect("failed.jsp");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
7.三个页面(index.html,success.jsp,failed.jsp)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="./login" method="post">
用户名:<input type="text" name="username" /> <br/>
密码:<input type="text" name="password" /> <br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1 style="color: green">登录成功..............</h1>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>失败</title>
</head>
<body>
<h1 style="color: red">登录失败.....................</h1>
</body>
</html>