一.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>