javaweb之浏览功能
今天我们来写浏览功能,浏览主要是通过sql语句将数据库里的数据查出来,并显示在页面上。
一.dao层
在上一篇文章的基础上dao层加入浏览方法。
public List<Course> list(){
String sql="select * from submitcourse";
List<Course> list=new ArrayList<>();
ResultSet rs=null;
Course ns=new Course();
try {
Connection conn=DBUtil.getConnection();
PreparedStatement pstmt=conn.prepareStatement(sql);
// pstmt.setString(1,n.getTeachername());
rs=pstmt.executeQuery();
while(rs.next())
{
ns=new Course(rs.getString(1),rs.getString(2),rs.getString(3));
list.add(ns);
}
//System.out.println(rs);
System.out.println(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
二.Servlet层
在servlet中实现交互,并修改servlet里面的格式。
package servlet; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; 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 Bean.Course;
import dao.daoCourse; /**
* Servlet implementation class course_servlet
*/
@WebServlet("/course_servlet")
public class course_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
daoCourse gooddao=new daoCourse();//调用dao层的方法
/**
* @see HttpServlet#HttpServlet()
*/
public course_servlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String method=request.getParameter("method");
if("add".equals(method))
{
String teachername=request.getParameter("teachername");
String classname=request.getParameter("classname");
String place=request.getParameter("place");
Course n=new Course(classname,teachername,place);
gooddao.add(n);
request.setAttribute("message", "添加成功");
request.getRequestDispatcher("NewFile2.jsp").forward(request, response);
}
}
else if("looklist".equals(method)) {
List<Course> nt= gooddao.list();
request.setAttribute("nt", nt);
System.out.println("王建民");
request.getRequestDispatcher("NewFile3.jsp").forward(request, response);
} } }
三.写jsp
在此处我们使用jstl和EL来进行数据的获取
NewFile3.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>课程名称</td>
<td>教师姓名</td>
<td>教学地点</td>
<td>操作</td>
</tr>
<c:forEach items="${nt}" var="xm">
<tr>
<td>${xm.classname}</td>
<td>${xm.teachername}</td>
<td>${xm.place}</td>
<td><a href="course_servlet?method=delete&teachername=${xm.teachername}" onClick="return confirm('确认删除吗?');">删除</a>
<a href="course_servlet?method=queryByTeachername&teachername=${xm.teachername}">修改</a>
</tr>
</c:forEach>
</table>
</body>
</html>
NewFile2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){ %>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<form action="course_servlet?method=looklist" method="post" >
<h1>添加成功</h1>
<input type="submit" value="浏览">
</form>
</body>
</html>
四.运行结果:
这写的有点丑,可以做一个菜单。

点击浏览即可浏览出表里面的数据。
下一篇文章为大家写删除功能!!!!!!
javaweb之浏览功能的更多相关文章
- Nginx 和 Apache 开启目录浏览功能
1.Nginx 在相应项目的 Server 段中的 location 段中,添加 autoindex on.例如: server { listen ; server_name www.dee.prac ...
- nginx和apache配置目录浏览功能
今天工作需要,要给客户提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让客户看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件: 我们都知道在apache下可以配置 ...
- Nginx目录浏览功能
要给其他人提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让其他人看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件:效果如图. 实现步骤:在虚拟主机配置文件里面开 ...
- 配置 Nginx 的目录浏览功能
Nginx 默认是不允许列出整个目录的,需要配置 Nginx 自带的 ngx_http_autoindex_module 模块实现目录浏览功能 . location / { alias /opt/fi ...
- Fiddler插件开发 - 实现网站离线浏览功能
有这么一种应用场景: 你是做前端或APP开发的,需要调用服务端提供的接口,接口只能在公司内网访问:在公司外就无法调试代码了. 想在公司外访问怎么办呢? 如果在公司的时候将所有接口的响应内容都保存起来, ...
- 启用Nginx目录浏览功能的方法
location / { root /data/www/file //指定实际目录绝对路径: autoindex on; ...
- BAT-给文件右击菜单增加7-ZIP浏览功能
Reg给文件右击菜单增加7-ZIP浏览功能 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\用7-ZIP浏览(ZJQ)] ...
- c# -- 实现浏览功能(备忘)
最近在做系统的时候,要实现浏览功能,但是由于本人记性一般,每次写完就忘,所以还是写篇随笔,备忘一下,方便以后查看@_@# 实现功能大概如下: 按钮1:点击浏览按钮后,选择文件(类型为.txt),默认位 ...
- 启动Nginx目录浏览功能及 让用户通过用户名密码认证访问web站点
一.启动Nginx目录浏览功能 [root@abcdocker extra]# cat w.conf server { listen 80; server_name IP地址; location / ...
随机推荐
- 自定义 serializers.ValidationError 的错误返回
在使用DRF进行反序列过程中,总是需要校验字段,然后返回错误结果.可以使用默认的自定义校验项,也可以自定义校验项.而默认的自定义校验项总是差强人意. 版本 Django 2.2.3 Python 3. ...
- 【人才】亚马逊数据科学家JD
Sr. Data Scientist - Amazon Homepage US, WA, Seattle • Job ID 271528 • Amazon Corporate LLC Job Desc ...
- 微信小程序授权登录将open_id传至后台并入库
要求能把用户昵称.头像以及open_id写入数据库,服务端保持用户登录状态 wxml: <block wx:else> <button type="primary" ...
- Docker修改默认IP
Docker修改默认IP 删除原有配置 sudo systemctl stop docker sudo ip link set dev docker0 down sudo brctl delbr do ...
- [apue] linux 文件系统那些事儿
前言 说到 linux 的文件系统,好多人第一印象是 ext2/ext3/ext4 等具体的文件系统,本文不涉及这些,因为研究具体的文件系统难免会陷入细节,甚至拉大段的源码做分析,反而不能从宏观的角度 ...
- Linux 查看文件大小并按照大小排序
使用df 命令查看当前系统磁盘的使用情况: [root@node ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/map ...
- java对配置文件properties的操作
1.读取配置文件的键值对,转为Properties对象:将Properties(键值对)对象写入到指定文件. package com.ricoh.rapp.ezcx.admintoolweb.util ...
- ModelSim10.7安装破解教程
我打算开始学习ZYNQ,所以先安装好工具ModelSim10.7,很无奈的是倒腾了一个上午也没生产license的txt文档和破解成功. 上面是破解失败的截图,下面是生成不了license,点了直接闪 ...
- 『忘了再学』Shell基础 — 4、Bash基本功能(history命令)
目录 1.history历史命令 2.设置命令历史记录的条数 3.清空历史命令 4.历史命令的调用 5.命令与文件的补全 在Linux系统中默认的Shell就是Bourne-AgainShell(简称 ...
- 【bjdctf】 BJD hamburger competition
是一个游戏 静态分析和动态分析都没思路 尝试查看运行时动态链接库 其中assembly-csharp.dll可能为实现游戏功能的动态链接库 .net逆向 Dnspy反编译 关键代码如上 Getflag ...