Javaweb服务器&客户端存储

目录

一,服务器是什么?

1.请求

2.转发

3.重定向

4.http

二,session与cookie的区别

1.session(后台存储)

session概念

session对象常见方法:

2.cookie(前端存储)

cookie概念

cookie常见方法

导包

创建Cookie

写入Cookie 

防止乱码

操作展示

三,七天免登陆案例

四,浏览记录

阅读界面(read)

浏览历史界面(history):

主页(index):



一,服务器是什么?

在这里,我们说的服务器通常是指Tomcat,而这里的Tomcat就相当于一个软件,当我们打开这个软件的时候,这个软件就会跑在我们的内存中。

其实服务器就相当于一个容器,用来装载我们的web项目。在这个容器中可以装载很多和web项目,web项目可以对外服务。

1.请求

在我们的浏览器上每次输入:

就相当于向我们的项目发送了一个请求,然后我们的项目就会返回我们的jsp文本,而此时浏览器就会编译jsp文本中的HTML代码,从而呈现出一个网页给我们。

2.转发

下面是图解:

 图中的bank就是一个银行,就行相当于一个项目,客户向我们的项目发送请求,项目把请求给柜台a(页面a),但是由于这个页面不具备这个功能,页面a就会把请求转发给页面b,以此类推,直到将请求转发到具备该功能的页面,该页面再一次响应请求,最后再通过页面a响应给客户。

3.重定向

下面是图解:

这里的每次访问请求是不一样的,与转发的不同之处就是发送了两次请求,而转发自始至终就只有一个请求。而且,重定向是不会携带数据过去的。

4.http

http是一个无状态的协议

在我们之前的web04项目中,doLogin中的

 就是转发的实现。

 由于HTTP是无状态的协议,上图中,客户去找柜台a补办卡,然后柜台a将卡的编号告诉客户,客户拿着编号去找柜台b取钱,柜台b拿这个编号去卡库进行查询,核实无误后就会拿钱给客户。

 这里session也是存储在内存中的,意思就是当你的电脑或者项目关闭的时候,你的卡库就不存在了。

session就是为了解决页面中的数据不一致。

二,session与cookie的区别

  • session(后台存储)

session概念

 Session在网络中被称为会话。
由于HTTP协议(超文本传输协议)是一种无状态协议,也就是当一个客户向服务器发出请求,服务器接收请求,并返回响应后,该连接就结束了,而服务器并不保存相关的信息。
为了弥补这一缺点,HTTP协议提供了Session。

通过Session可以在应用程序的WEB页面间进行跳转时,保存用户的状态,使整个用户会话一直存在下去,直到关闭浏览器。


session对象常见方法:

类型

方法名称

说  明

void

setAttribute(String key,Object value)

以key/value的形式保存对象值

Object

getAttribute(String key)

通过key获取对象值 

int

getMaxInactiveInterval()

获取session的有效非活动时间,以秒为单位

String

getId()

获取session对象的编号

void

invalidate()

设置session对象失效

  • cookie(前端存储)

cookie概念

Cookie的中文意思是“小甜饼”,然而在互联网上的意思与这就完全不同了。它和食品完全没有系。  在互联网中,Cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器。通过使用   cookie可以标识用户身份,记录用户名和密码,跟踪重复用户等。浏览器将cookie以key/value  的形式保存到客户机的某个指定目录中。
通过cookie的getCookie()方法即可获取到所有cookie对象的集合;

通过cookie对象的getName()方法可以获取到指定名称的cookie; 

通过getValue()方法即可获取到cookie对象的值。

另外,将一个cookie对象发送到客户端,使用response对象的addCookie()方法。
 

cookie常见方法

类型

方法名称

说  明

void

setMaxAge(int expiry)

设置Cookie的有效期,以秒为单位

void

setValue(String value)

在Cookie创建后,对Cookie进行赋值 

String

getName()

获取Cookie的名称

String

getValue()

获取Cookie的值

String

getMaxAge()

获取Cookie的有效时间,以秒为单位

 tip:虽然我们没有删除cookie的方法,但是可以将cookie的过期时间设置为<=0就能够时cookie无效了。

导包

import="javax.servlet.http.Cookie"

创建Cookie

Cookie newCookie=new Cookie("parameter", "value");

parameter:用于代表cookie的名称(key)

value:用于表示当前key名称所对应的值

写入Cookie 

response.addCookie(newCookie)

防止乱码

为了放置乱码一般要进行线面两个操作:

1.放进入之前进行编码:
String username1 = URLEncoder.encode(username, "utf-8");

2.取值的时候进行解码:
String username1 = URLDecoder.decode(cookies[i].getValue(),"utf-8"); 

操作展示

我们右键网页检查:

 然后直接在浏览器上再次访问发现,每次访问的值都不一样。意思就是session不一样了,但是并没有消失,只是存储在前端cookie中。

如果在我们的web04项目中不登录,主页中的name就会显示null值,接下进行一个操作,右键检查网页,找到上面我们提到的值将原本的值覆盖到这上面刷新页面发现,有name了。那么到这里就会发现并不是每一次都会在后台生成一个session id,是因为没有进行数据存储,给你卡号不是浪费吗?

输入一下代码检查网页发现:

 

 会存储到session中

注意:这里的pwd和name只会在浏览器打开的时候有效,因为cookie只在浏览器打开的时候有用,就算再开一个浏览器,用的还是这个cookie因为之前那个浏览器还没有关闭的。

三,七天免登陆案例

代码如下:

login:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%
	/**
	*登录界面
	*/
	%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
	outline: none !important;
}

html, body {
	background:#1abe9c;
}

form {
	width: 300px;
	background: #ebeff2;
	box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
	border-radius: 5px;
	padding: 20px;
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
}

.btn-group {
	width: 100%;
}

.btn-group button {
	width: 50%;
}
</style>
</head>
<%
		//用来存储数据
		String name="";
		String pwd="";
 		//如果客户端没有cookie,那么会报控制异常
		if(request.getCookies()!=null)
			for(Cookie cookie:request.getCookies()){
				if(cookie.getName().equals("name")){
					name=cookie.getValue();//得到值
				}
				if(cookie.getName().equals("pwd")){
					pwd=cookie.getValue();
				}
			}
		
%>
<body>
	<!-- methid的方式有两种:1.get(直接显示在状态栏上的) 2.post(不会显示在状态栏上的数据) -->
	<form action="doLogin.jsp" method="post" id="myForm">
		<h3 class="text-center">欢迎使用一麟新闻管理</h3>
		<div class="form-group">
			<!-- 在输入框将数据填入(使用value属性) -->
			<input name="name" value="<%=name%>" type="text" id="username" class="form-control" placeholder="请输入您的姓名">
		</div>
		<div class="form-group">
			<input name="password" value="<%=pwd%>" type="password" id="password" class="form-control" placeholder="请输入您的密码">
		</div>
		<div class="btn-group">
			<button type="submit" class="btn btn-primary">登录</button>
			<button type="button" class="btn btn-danger" onclick='location=href="regiest.jsp"'>没有账号?</button>
		 	<!-- 没有账号就跳转到注册的界面 -->
		</div>
	</form>
	<script type="text/javascript">
	//表单的提交验证
	//tip:这里只是做了非空判断,这里能使用很多方式做验证,比如jQuery插件,JavaScript验证,jQuery验证
	$("#myForm").submit(()=>{
		if($("#username").val().lenght==0){
			alert("用户名不能为空");
			return false;
		//return false;这个时候表单是不提交的
		}if($("#password").val().lenght==0){
			alert("密码不能为空");
			return false;
		}
	})
	</script>
</body>
</html>

dologin:

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
/**
*登录数据库处理
*/
request.setCharacterEncoding("UTF-8");//设置编码语言,避免乱码
String name = request.getParameter("name");//从登录界面通过输入框中的name属性获取用户姓名
String pwd = request.getParameter("password");//从登录界面通过输入框中的name属性获取用户密码
//导包
//OracleDriver(alt+/)
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String URL = "jdbc:oracle:thin:@localhost:1521:orcl";//可以封装成类
//获得连接
Connection con = DriverManager.getConnection(URL, "scott", "sa123");
//获取执行对象
PreparedStatement ps = con.prepareStatement("select * from t_user where user_name=? and user_password=?");
//占位符设值
ps.setString(1, name);
ps.setString(2, pwd);
//获得结果集
ResultSet rs = ps.executeQuery();
//判断结果
if (rs.next()) {
	//将用户名存储到服务器中的session中
	session.setAttribute("username", name);//到session中去拿值,数据的名字是username,而对应的值是name
	//这里的name存储到后台内存中去了
	//cookie的值每次发送请求的时候回自动携带的,只是不可见
	//cookie默认是在浏览器的过程中生效的,意思是只要浏览器没有关,cookie是一只生效的
	Cookie cookie01=new Cookie("name",name);
	Cookie cookie02=new Cookie("pwd",pwd);
	cookie01.setMaxAge(60*60*24*7);//设置存活时间(单位是秒)
	cookie02.setMaxAge(60*60*24*7);
	//存储到前台中
	response.addCookie(cookie01);
	response.addCookie(cookie02);
	response.sendRedirect("news/index.jsp");
	//request.getRequestDispatcher("/news/index.jsp").forward(request, response);
	/**
	在下述讲解中,我们就会发现我们的/news/index.jsp路径在页面中的跳转也是能够成功的,
	那么问题来了,按照理论知识来讲是不能进行跳转的,而且路径也该会变成
	localhost:8080/news/index.jsp
	其实原因很简单:
	 	我们的转发是属于服务器行为,而重定向则不然,是属于客户端行为,注意两者的区别!
	 	说到这里,我们就不难理解了,因为在此无论如何都是以项目为主的,就会跳转到我们项目的根目录,
	 	路径应该是localhost:8080/web04/news/index.jsp
	**/
} else {
	//重定向
	/**
	   跳转的时候有两种情况:
	   (1)a.jsp  跳转到当前路径下的a.jsp,假如当前路径是localhost:8080/web04,
	      那么就会跳转到localhost:8080/web04/a.jsp
	   (2)../a.jsp 跳转到上一级路径下的a.jsp 就上述路径而言,
	      就会跳转到localhost:8080/a.jsp
	   (3)/a.jsp 根目录的a.jsp 就上述路径而言,根目录是localhost:8080
	      那么就会跳转到loaclhost:8080/a.jsp
	   
	**/
	response.sendRedirect("login.jsp");//或者路径为login.jsp,因为是同级目录 
}
//关闭资源
if (con != null && !con.isClosed()) {
	con.close();
}
if (ps != null) {
	ps.close();
}
if (rs != null) {
	rs.close();
}
%>

我们可以右键检查我们的网页看到免登陆的过期时间

当然这个免登陆还很简陋,而且还有缺点:

存储在cookie中不安全,能够在网页中获取你的数据。

四,浏览记录

阅读界面(read)

<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.PreparedStatement"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.Connection"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<link
	href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css"
	rel="stylesheet">
<script
	src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script
	src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
	outline: none !important;
}

body, html {
	background: #7f8d90;
}

nav, .breadcrumb {
	border-radius: 0 !important;
	margin-bottom: 0 !important;
}

.breadcrumb {
	margin-bottom: 20px !important;
	background: #36485c;
	color: white;
}

input, select, textarea, .panel-heading {
	border: none !important;
	border-radius: 0 !important;
}

.breadcrumb .active {
	color: yellow;
}
</style>
</head>
<%
	Object username=session.getAttribute("username");
	if(username==null){
		response.sendRedirect("${pageContext.request.contextPath}/login.jsp");
	}
%>
<body>
	<nav class="navbar navbar-default hidden-sm hidden-xs">
		<div class="container-fluid">
			<div class="navbar-header">
				<a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp" style="font-size: 25px;">🐖</a>
			</div>
			<ul class="nav navbar-nav">
				<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理 <span class="caret"></span></a>
					<ul class="dropdown-menu">
						<li><a href="${pageContext.request.contextPath}/news/add.jsp">新闻发布</a></li>
						<li class="divider"></li>
						<li><a href="${pageContext.request.contextPath}/news/show.jsp">类别管理</a></li>
					</ul>
				</li>
			</ul>
			<ul class="nav navbar-nav navbar-right">
				<li><a><%=session.getAttribute("username")%></a></li>
				<li><a href="${pageContext.request.contextPath}/news/history.jsp>">历史记录</a><li>
				<li><a href="${pageContext.request.contextPath}/login.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
			</ul>
		</div>
	</nav>

	<ol class="breadcrumb">
		<li>您当前的位置是</li>
		<li>新闻发布系统</li>
		<li class="active">新闻阅读</li>
	</ol>
	<%
	//去session拿历史浏览
	Object obj=session.getAttribute("historyList");
	List<String> historyList=new ArrayList<>();
	if(obj!=null){//判断是否有历史列表
		historyList=(List<String>)obj;
	}
	//获得新闻的id
	String newId = request.getParameter("newId");//从主页获取新闻编号
	//拿到cookie
	String history="";
	for(Cookie cookie:request.getCookies()){
		if(cookie.getName().equals("historyList")){
			history=cookie.getValue();
		}
	}
	history+=newId+",";
	response.addCookie(new Cookie("historyList",URLEncoder.encode(history, "utf-8")));
	//根据新闻id去数据库做查询操作
	//oracledriver
	//加载驱动
	Class.forName("oracle.jdbc.driver.OracleDriver");
	//定义连接字符串
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con = DriverManager.getConnection(url, "scott", "sa123");
	//查询所有新闻数据
	PreparedStatement ps = con.prepareStatement("select * from T_NEWS where NEWS_ID=?");
	//设置占位符
	ps.setInt(1, Integer.parseInt(newId));
	//获得结果集
	ResultSet rs = ps.executeQuery();
	//定义初始值
	String title = "";
	int count = 0;
	String author = "";
	String publisher = "";
	String content = "";
	if (rs.next()) {
		title = rs.getString(2);
		publisher = rs.getString(5);
		author = rs.getString(4);
		content = rs.getString(6);
		count = rs.getInt(8) + 1;//指的是当前自己也阅读了一次
	}
	//将阅读信息存储到集合中
	historyList.add(title+"@"+count);
	//将历史记录集合重新放到session中去
	session.setAttribute("historyList", historyList);
	//已经阅读过了->修改阅读次数
	ps = con.prepareStatement("update t_news set news_count=news_count+1 where news_id=?");
	ps.setInt(1, Integer.parseInt(newId));
	ps.executeUpdate();//无需判断,无论如何都要进行下去的
	%>
	<div class="container"
		style="background: rgba(239, 231, 231, 0.9); border-radius: 10px;">
		<h1><%=title%></h1>
		<h3 class="text-right">
			<small> <span class="glyphicon glyphicon-user"><span
					class="label label-default"><%=author%></span></span> <span
				class="glyphicon glyphicon-eye-open"><span
					class="label label-default"><%=count%></span></span> <span
				class="glyphicon glyphicon-time"><span
					class="label label-info"><%=publisher%></span></span>
			</small>
		</h3>
		<samp><%=content%></samp>
		<div class="btn-group btn-group-justified"
			style="margin-bottom: 20px;">
			<div class="btn-group">
				<a href="${pageContext.request.contextPath}/news/doDelete.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a>
			</div>
			<div class="btn-group">
				<a href="${pageContext.request.contextPath}/news/update.jsp?newId=<%=newId%>" class="btn btn-info" type="button">修改</a>
			</div>
		</div>
	</div>

	<div class="container" style="background: rgba(239, 231, 231, 0.9); border-radius: 10px; margin-top: 10px;">
		<%
		ps = con.prepareStatement("select * from t_comment where comment_from=?");
		ps.setInt(1, Integer.parseInt(newId));
		rs = ps.executeQuery();
		while (rs.next()) {
		%>
		<div class="panel panel-default" style="margin-top: 20px;">
			<div class="panel-heading">
				<span class="glyphicon glyphicon-user"><span class="label label-success"><%=rs.getString(4) %></span></span>
				<p style="margin-top: 10px; text-indent: 2em;">
					<samp><%=rs.getString(5)%></samp>
					<!-- 显示评论的内容 -->
				</p>
				<p class="text-right">
					<span class="glyphicon glyphicon-time"><span class="label label-info"><%=rs.getString(3) %></span></span>
				</p>
				<a href="${pageContext.request.contextPath}/news/doDelPl.jsp?newId=<%=newId%>&&id=<%=rs.getInt(1)%>">删除</a>
			</div>
			<%
			}
			%>

			<form action="doAddPl.jsp" class="container" style="background: rgba(239, 231, 231, 0.9); border-radius: 10px; margin-top: 10px; padding: 30px;">
				<input type="hidden" name="newId" value="<%=newId%>">
				<div class="form-group">
					<label for="name">Name</label> <input name="author" class="form-control" placeholder="用户名称" required type="text">
				</div>
				<div class="form-group">
					<label for="email">content</label> <input name="content" class="form-control" placeholder="评论内容" required type="text">
				</div>
				<button class="btn btn-default" type="submit">发布评论</button>
			</form>

			<div style="height: 50px;"></div>
</body>
</html>

浏览历史界面(history):

<%@ page import="java.util.List" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
/**
*浏览历史记录界面
*/
%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
</head>

<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" style="font-size: 25px;">当前是您的浏览记录</a>
        </div>
        <ul class="nav navbar-nav navbar-right" style="margin-right: 20px;">
            <li><a href="${pageContext.request.contextPath}/news/index.jsp">返回首页</a></li>
        </ul>
    </div>
</nav>

<div class="container">
    <ul class="list-group">
        <%
            //从cookie中取历史记录
            String history="";
            for (Cookie cookie : request.getCookies()) {
                if(cookie.getName().equals("historyList")){
                	history= URLDecoder.decode(cookie.getValue(),"utf-8"); ;
                }
            }
            //不是从数据库来的 从session中拿到的
            Object obj = session.getAttribute("historyList");
            if(obj!=null){
                List<String> historyList=(List<String>)obj;
                for (int i=historyList.size()-1;i>=0;i--) {
                    //倒着来
                    String[] ss=historyList.get(i).split("@");
        %>
        <li class="list-group-item">
            <span class="badge"><%=ss[1]%></span>
            <%=ss[0]%>
        </li>
        <%
                }
            }
        %>
    </ul>
</div>
</body>
</html>

主页(index):

<%@page import="javax.servlet.http.Cookie"%>
<%@page import="java.nio.charset.StandardCharsets"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
/**
*新闻主页
*/
%>

<!DOCTYPE html>
<html lang="zh">

<head>
<meta charset="UTF-8">
<title>bootstrap</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script
	src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script
	src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
	outline: none !important;
}

body, html {
	background: #7f8d90;
}

nav, .breadcrumb {
	border-radius: 0px !important;
	margin-bottom: 0px !important;
}

.breadcrumb {
	margin-bottom: 20px !important;
	background: #36485c;
	color: white;
}

li h4 {
	width: 300px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.breadcrumb .active {
	color: yellow;
}
</style>
</head>

<body>
	<nav class="navbar navbar-default hidden-sm hidden-xs">
		<div class="container-fluid">
			<div class="navbar-header">
				<a class="navbar-brand" href="index.jsp" style="font-size: 25px;">🐖</a>
			</div>
			<ul class="nav navbar-nav">
				<li class="dropdown"><a class="dropdown-toggle"
					data-toggle="dropdown"> 新闻管理 <span class="caret"></span>
				</a>
					<ul class="dropdown-menu">
						<li><a href="${pageContext.request.contextPath}/news/add.jsp">新闻发布</a></li>
						<li class="divider"></li>
						<li><a href="${pageContext.request.contextPath}/news/show.jsp">类别管理</a></li>
					</ul></li>
			</ul>
			<ul class="nav navbar-nav navbar-right">
				<li><a><%=session.getAttribute("username")%>></a></li>
				<!-- 从会话session中获取数据 -->
				<li><a href="${pageContext.request.contextPath}/news/history.jsp">历史记录</a></li>	
				<li><a href="doExit.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
			</ul>
		</div>
	</nav>

	<ol class="breadcrumb">
		<li>您当前的位置是</li>
		<li>新闻发布系统</li>
		<li class="active">首页</li>
	</ol>

	<form class="form-inline" style="margin: 0px auto 20px;">
		<div class="form-group" style="display: block; text-align: center;">
			<div class="input-group">
				<div class="input-group-addon">新闻标题</div>
				<input type="text" name="newName" class="form-control" placeholder="请在此输入搜索的关键字">
				<span class="input-group-btn">
					<button type="submit" class="btn btn-primary">搜索🔍</button>
				</span>
			</div>
		</div>
	</form>

	<div class="container">
		<ul class="list-group">
			<%
			//点击表单后应该在页面上携带newname用来充当查询的关键字
			String newName = request.getParameter("newName");
			if (newName == null) {
				newName = "";//进行查询所有
			}
			//拿值的时候会发现出现乱码的情况,这个时候可以将数据先变成字节的形式再变成字符的形式(破碎重组)
			
			//newName = new String(newName.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			//OracleDriver
			//加载驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//连接字符串
			String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
			//获得连接
			Connection con = DriverManager.getConnection(URL, "scott", "sa123");
			//查询所有的新闻数据
			PreparedStatement ps = con.prepareStatement("select * from t_news where news_title like ?");
			ps.setString(1, "%" + newName + "%");
			//得到结果集
			ResultSet rs = ps.executeQuery();
			//遍历结果集
			while (rs.next()) {
			%>
			<li class="list-group-item">
				<h4 class="list-group-item-heading">
					<a href="${pageContext.request.contextPath}/news/read.jsp?newId=<%=rs.getInt(1)%>" data-placement="bottom" data-toggle="tooltip" href="" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例"> <%=rs.getString(2)%></a>
				</h4>
				<p class="list-group-item-text text-right">
					<span class="glyphicon glyphicon-user"><code><%=rs.getString(4)%></code></span>
					<span class="glyphicon glyphicon-eye-open"><code><%=rs.getInt(8)%></code></span>
					<span class="glyphicon glyphicon-tag"><code><%=rs.getInt(9)%></code></span>
					<span class="glyphicon glyphicon-time"><code><%=rs.getString(5)%></code></span>
				</p>
			</li>
			<%
			}
			//资源的关闭
			if (con != null && !con.isClosed()) {
			con.close();
			}
			if (ps != null) {
			ps.close();
			}
			if (rs != null) {
			rs.close();
			}
			%>
		</ul>
	</div>
	<div class="container text-center">
		<ul class="pagination" style="margin: 20px auto;">
			<li><a href="#"><span>&laquo;</span></a></li>
			<li><a href="#">1</a></li>
			<li><a href="#">2</a></li>
			<li><a href="#">3</a></li>
			<li><a href="#">4</a></li>
			<li><a href="#">5</a></li>
			<li><a href="#"><span>&raquo;</span></a></li>
		</ul>
	</div>
	<script>
		$(function() {
			$('[data-toggle="tooltip"]').tooltip({
				trigger : "hover"
			})
		})
	</script>
</body>
</html>

效果图:

 

 这期博客主要完成的七天免登陆的功能还有浏览历史的功能。下期会继续完善我们的web04(新闻发布系统)的其他功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一麟yl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值