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个库表是数据库系统自带的表,一般不放数据. 进入某个库 切换库,并显示库 ...
随机推荐
- array numpy 模块
高级用法:http://www.jb51.net/article/87987.htm from array import * 调用 array 与 import numpy as np 调用 np. ...
- oracle 网络环境配置
PLSQL Developer连接Oracle11g 64位数据库配置详解 最近换了台64bit的电脑,所以oracle数据库也跟着换成了64bit的,不过 问题也随之产生,由于plsql devel ...
- Mycat入门及简单规则
以下都来自网络: 官网: http://www.mycat.io/ 官网配置文件解析: https://github.com/MyCATApache/Mycat-Server/wiki/9.0-%E6 ...
- 获取RequestMapping注解中的属性
参考:https://www.cnblogs.com/2013jiutian/p/7294053.html @RequestMapping("/value1") @Controll ...
- php71 gdnz
更新yum库:yum updat yum install epel-release yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel ...
- ubuntu 下当前网速查看
ubuntu下用ethstatus可以监控实时的网卡带宽占用.这个软件能显示当前网卡的 RX 和 TX 速率,单位是Byte 一.安装 ethstatus 软件 #sudo apt-get insta ...
- 全国省市区数据库SQL(有可能不是最新的)
百度云下载地址:https://pan.baidu.com/s/1lStN7tYpwOtpC-r3G2X2sw
- SSL - 简介
一.密码技术 要了解SSL协议,首先要了解:加密算法.消息摘要算法(又称为哈希算法Hash),数字签名等概念.这些技术每个都可以写出一整本的书,它们结合在一起,提供了保密性.完整性和身份验证的功能. ...
- qt基本类
多firstpage secondpage thirdpage fouthpage 实现 多页面 xml解析 实现 按钮 和 slot实现 mysql数据库访问实现
- 2018.07.25 bzoj3878: [Ahoi2014&Jsoi2014]奇怪的计算器(线段树)
传送门 线段树综合. 让我想起一道叫做siano" role="presentation" style="position: relative;"&g ...