0%

JavaWebCode

第五章 Cookie 和 Session

1.蛋糕商城

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class PurchaseServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = (String) req.getAttribute("id");
if(id==null){
resp.sendRedirect("/ListBookServlet");
return;
}

Book book = BookDB.getBook(id);
HttpSession session = req.getSession();
List<Book>cart = (List)session.getAttribute("cart");

if(cart == null){
cart = new ArrayList<Book>();
session.setAttribute("cart");
}
cart.add(book);

Cookie cookie = new Cookie("JSESSIONID",session.getID());
cookie.setMaxAge(60*30);
cookie.setPath("/chapter06");
resp.addCookie(cookie);

resp.sendRedirect("BookServlet");
}
}

2.显示上次访问时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@WebServlet(name="lastAccessServlet",urlPatterns="/lastAccessServlet")
public class LastAccessServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");

String lastAccessTime = null;
Cookie[] cookies = req.getCookies();

for(Cookie cookie : cookies){
if("lastAccess".equals(cookie.getName())){
lastAccessTime = cookie.getValue();
break;
}
resp.getWriter().println(cookie.getName());
}

if(lastAccessTime == null){
resp.getWriter().print("您是首次访问!");
}else{
resp.getWriter().print("您上次访问的时间是:" + lastAccessTime);
}

Cookie cookie = new Cookie("lastAccess", new Date().toString());
cookie.setMaxAge(60*60);
cookie.setPath("/School_war_exploded");
resp.addCookie(cookie);
}
}

第六章 JSP

1.显示时间

time.jsp

1
2
3
4
5
6
7
8
9
10
11
<%@ page contentType ="text/html;charset=utf-8" %>
<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>

欢迎访问 , 现在的时间是:
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String str = sdf.format(new Date());
%>

<%= str%>

time_main.jsp

1
2
3
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<jsp:include page = "time.jsp" />

2.写两个jsp,要求显示b,等待5s显示a

a.jsp

1
2
3
4
<%@ page contentType="text/html;charset=utf-8" %>

<% Thread.sleep(5000); %>
a.jsp中的中文

b.jsp

1
2
3
4
<%@ page contentType="text/html;charset=utf-8" %>

b.jsp的中文
<jsp:include page="a.jsp" flush="true" />

第八章

UserBean

注意属性不能大写!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package Entity;

public class User {
private String username = "zhangsan";
private int age = 20;
private String sex = "nan";

public int getAge() {
return age;
}
public String getUsername(){
return username;
}
public String getSex() {
return sex;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setUsername(String username) {
username = username;
}
}

add.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2024-01-02
Time: 22:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="info.jsp" method="get">
姓名:<input type="text" name="username"> <br>
年龄:<input type="text" name="age"> <br>
性别:<input type="text" name="sex"> <br>
<input type="submit" value="添加">
</form>
</body>
</html>

info.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<%@ page contentType="text/html;charset=utf-8" %>

<html>
<head>
<title>title</title>
</head>

<body>
<% request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="user" class="Entity.User" scope="page">
// 通过表单提交赋值
<jsp:setProperty name="user" property="*"></jsp:setProperty>
</jsp:useBean>

// 通过直接赋值
<jsp:setProperty name="user" property="username" value="skyang" />
<jsp:setProperty name="user" property="age" value="111" />
<jsp:setProperty name="user" property="sex" value="男" />

<table>
<tr>
<td>姓名: <jsp:getProperty property="username" name="user"/> </td>
</tr>
<tr>
<td> 年龄: <jsp:getProperty property="age" name="user" /> </td>
</tr>
<tr>
<td> 性别: <jsp:getProperty property="sex" name="user"/></td>
</tr>
</table>
</body>

</html>

第十章

1.基本步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void main(String[] args) throws SQLException{
// 1.加载数据库驱动
DriverManager.registerDriver(new com.example.mysql.jdbc.Driver());

// 2.通过DriverManager获取数据库链接
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "skyang";
String password = "skyang";
Connection conn = DriverManager.getConnection(url,username,password);

// 3.通过Connection对象获取Statement对象
Statement stmt = conn.createStatement();

// 4.使用Statement独享执行SQL语句
String sql = "select * from users";
ResultSet rs = stmt.executeQuery(sql);

// 5.操作ResultSet结果集
while(rs.next()){
String name = rs.getString("name");
String word = rs.getString("password");
}

// 6.回收数据库资源
rs.close();
stmt.close();
conn.close();
}

2.PrepareStatement

1
2
3
4
5
6
conn = JDBCUtils.getConnection();
String sql = "select password from users where username= ?";
ps = conn.prepareStatement(sql);

ps.setString(1, user.getUsername());
rs = ps.executeQuery();

3.JDBCUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package Utils;

import java.sql.*;

public class JDBCUtils {
public static Connection getConnection() throws SQLException,ClassNotFoundException{
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/bloguser?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8";
String username = "root";
String password = "skyang9";

Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}

public static void release(PreparedStatement stmt , Connection conn){
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}

public static void release(ResultSet rs , PreparedStatement stmt , Connection conn){
if(rs != null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
release(stmt,conn);
}
}

UserDAO部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static String Find(User user){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = JDBCUtils.getConnection();
String sql = "select password from users where username= ?";
ps = conn.prepareStatement(sql);

ps.setString(1, user.getUsername());
rs = ps.executeQuery();

String SQLpassword;
while(rs.next()){
SQLpassword = rs.getString("password");
return SQLpassword;
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(rs,ps,conn);
}
return null;
}