java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)
编写过程中需要注意的问题:
1.建立eclipse动态web工程时,需要改写编译后class文件的位置,通常情况下是这个位置:/WebContent/WEB-INF/classes
2.配置的页面链接和servlet类之间有两种方式:
1)通过在web.xml文件中进行配置:示例如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jspshow1</display-name> <servlet>
<servlet-name>listTheStudent</servlet-name>
<servlet-class>com.guodiantong.javaweb.ListTheStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>listTheStudent</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deleteTheStudent</servlet-name>
<servlet-class>com.guodiantong.javaweb.DeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deleteTheStudent</servlet-name>
<url-pattern>/delete</url-pattern>
</servlet-mapping>
</web-app>
2)通过在eclipse新建servlet时,自动指示设置页面请求和servlet类之间的链接关系:如下图所示
new--->servlet

这种也能实现请求和对应的servlet类之间的映射,通过这种方式你会发下,tomcat使用的注解的方式@WebServlet,这种方式在web.xml文件中并没有通过 1)的那种方式显现出来
谈完上述注意的地方,下面开始见工程:先看下项目的目录结构

先看下所有的jsp文件:test.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="list">list all students</a>
</body>
</html>
students.jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@page import="com.guodiantong.javaweb.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%=request.getAttribute("students") %>
<br>
<% List<Student> stus=(List<Student>)request.getAttribute("students");
%>
<table>
<tr>
<th>flowid</th>
<th>type</th>
<th>id_card</th>
<th>exam_card</th>
<th>studentname</th>
<th>location</th>
<th>grade</th>
<th>操作</th>
<%
for(Student student:stus){ %>
<tr>
<td><%=student.getFlowid() %></td>
<td><%=student.getType() %></td>
<td><%=student.getIdcard() %></td>
<td><%=student.getExam_card() %></td>
<td><%=student.getStudentname() %></td>
<td><%=student.getLocation() %></td>
<td><%=student.getGrade() %></td>
<td><a href="delete?flowid=<%=student.getFlowid() %>">删除</a></td>
</tr> <%
}
%>
</table> </body>
</html>
success.jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
操作成功!!
<a href="list">refresh</a>
</body>
</html>
下面是servlet类的代码:List请求对应的servlet类:ListTheStudent.java
package com.guodiantong.javaweb; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ListTheStudent extends HttpServlet { @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Student> students=new ArrayList<Student>();
StudentDao studentDao=new StudentDao();
students=studentDao.getAll();
request.setAttribute("students", students);
request.getRequestDispatcher("/students.jsp").forward(request, response);
} }
delete请求对应的jservlet java类:DeleteServlet.java
package com.guodiantong.javaweb; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DeleteServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String flowid=request.getParameter("flowid");
StudentDao studentDao=new StudentDao();
studentDao.deleteStudent(flowid);
request.getRequestDispatcher("/success.jsp").forward(request, response);
} }
项目中最重要的StudentDao.java
package com.guodiantong.javaweb; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class StudentDao {
public List<Student> getAll(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
List<Student> students=new ArrayList<Student>(); try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql:///dsm";
String user="root";
String password="12345678";
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
String sql="SELECT flow_id,Type,id_card,exam_card,student_name,location,grade "
+"FROM examstudent";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
String flowid=resultSet.getString(1);
System.out.println(flowid); String type=resultSet.getString(2);
String idcard=resultSet.getString(3);
String exam_card=resultSet.getString(4);
String studentname=resultSet.getString(5);
String location=resultSet.getString(6);
String grade=resultSet.getString(7);
Student student=new Student(flowid, type, idcard, exam_card,
studentname, location, grade);
students.add(student); }
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(resultSet !=null){
resultSet.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(preparedStatement !=null){
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(connection !=null){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return students;
} public void deleteStudent(String flowid){
Connection connection=null;
PreparedStatement preparedStatement=null; try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql:///dsm";
String user="root";
String password="12345678";
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
String sql="DELETE FROM examstudent where flow_id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, flowid);
preparedStatement.execute(); } catch (Exception e) {
e.printStackTrace();
}finally{ try {
if(preparedStatement !=null){
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(connection !=null){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
项目中的javabean :Student.java
package com.guodiantong.javaweb;
public class Student {
private String flowid;
private String type;
private String idcard;
private String exam_card;
private String studentname;
private String location;
private String grade;
public String getFlowid() {
return flowid;
}
public void setFlowid(String flowid) {
this.flowid = flowid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getExam_card() {
return exam_card;
}
public void setExam_card(String exam_card) {
this.exam_card = exam_card;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Student() {
super();
}
public Student(String flowid, String type, String idcard, String exam_card,
String studentname, String location, String grade) {
super();
this.flowid = flowid;
this.type = type;
this.idcard = idcard;
this.exam_card = exam_card;
this.studentname = studentname;
this.location = location;
this.grade = grade;
}
@Override
public String toString() {
return "Student [flowid=" + flowid + ", type=" + type + ", idcard="
+ idcard + ", exam_card=" + exam_card + ", studentname="
+ studentname + ", location=" + location + ", grade=" + grade + "]";
}
}
提到javabean就需要看一下后台数据表格的结构:

就是这些!!
java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)的更多相关文章
- java web学习笔记-Servlet篇
Servlet基础 1.Servlet概述 JSP的前身就是Servlet.Servlet就是在服务器端运行的一段小程序.一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问 ...
- 3 Java Web 入门 1 Servlet 入门
1 Tomcat 1.1 安装 JDK Oracle 官网 1.2 安装 Tomcat
- Java web 入门知识 及HTTP协议详解
Java web 入门知识 及HTTP协议详解 WEB入门 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资 ...
- Java Web入门经典扫描版
全书共分4篇19章,其中,第一篇为“起步篇”,主要包括开启JavaWeb之门.不可不知的客户端应用技术.驾驭JavaWeb开发环境.JavaWeb开发必修课之JSP语法等内容:第二篇为“核心篇”,主要 ...
- Java快速入门-02-基础篇
Java快速入门-02-基础篇 上一篇应该已经让0基础的人对 Java 有了一些了解,接一篇更进一步 使用 Eclipse 快捷键 这个老师一般都经常提,但是自己不容易记住,慢慢熟练 快捷键 快捷键作 ...
- Java快速入门-01-基础篇
Java快速入门-01-基础篇 如果基础不好或者想学的很细,请参看:菜鸟教程-JAVA 本笔记适合快速学习,文章后面也会包含一些常见面试问题,记住快捷键操作,一些内容我就不转载了,直接附上链接,嘻嘻 ...
- java rmi 入门实例
java rmi 入门实例 (2009-06-16 16:07:55) 转载▼ 标签: java rmi 杂谈 分类: java-基础 java rmi即java远程接口调用,实现了2台虚拟机之 ...
- Javaweb项目-下拉列表显示后台数据库的数据
下面将演示前端下拉列表显示后台数据库中class表的说有班级的名称 环境: Tomcat-8.5.40 mysql-8.0.13 eclipse-4.9.0 springmvc框架 一.从mysql中 ...
- MariaDB——显示所有数据库列表
显示所有数据库列表:其中,information_schema.performance_schema.test.mysql,这4个库表是数据库系统自带的表,一般不放数据. 进入某个库 切换库,并显示库 ...
随机推荐
- pip & Jinja2
[pip&Jinja2] 2.7.9 以及上版本的 python 安装程序会自动安装 pip. 升级:pip install --upgrade pip. 使用pip安装Jinja2:sudo ...
- ThreadPoolExecutor常识
线程池技术在并发时经常会使用到,java中的线程池的使用是通过调用ThreadPoolExecutor来实现的.ThreadPoolExecutor提供了四个构造函数,最后都会归结于下面这个构造方法: ...
- iOS 基于MVC设计模式的基类设计
iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 20-java 对象链表空没空呢
写了一个 对象链表,往里面add了一些对象,最后我想看下链表是否为空,用 == null 为假,也看不出, 看下长度? 好吧, size() = 1: 打印 null , 那到底是不是空 啊, ...
- 只有自己看的懂的vue 二叉树的3级联动
我是在vue做的数据 actions mutations state index页面获取值 传递给子页面 子页面的操作 <template> <div class='cascade_ ...
- tomcat用虚拟目录方式发布项目与manager页面配置
conf/Catalina/localhost:指定项目的配置信息 1.添加:ROOT.xml 听见Context节点: <Context docBase="/usr/local/to ...
- Luogu 3960 [NOIP2017] 列队 - splay|线段树
题解 是我从来没有做过的裂点splay... 看的时候还是很懵逼的QAQ. 把最后一列的$n$个数放在一个平衡树中, 有 $n$ 个点 剩下的$n$行数, 每行都开一个平衡树,开始时每棵树中仅有$1$ ...
- 社交类APP原型模板分享——微信
微信是一款社交类的APP应用——聊天软件,支持多人群聊. 交互效果主要有滚动内容界面.选择组件触发按钮状态变化.点击下拉展开列表.点击弹出面板等交互操作. 本原型由国产原型工具-Mockplus制作完 ...
- ipconfig 查看本机IP地址
打开cmd 窗口 然后输入ipconfig 就会为你展示你想要的IP地址了...