编写过程中需要注意的问题:

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篇(显示后台数据库列表,删除某一条记录并显示)的更多相关文章

  1. java web学习笔记-Servlet篇

    Servlet基础 1.Servlet概述 JSP的前身就是Servlet.Servlet就是在服务器端运行的一段小程序.一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问 ...

  2. 3 Java Web 入门 1 Servlet 入门

    1 Tomcat 1.1 安装 JDK Oracle 官网 1.2 安装 Tomcat

  3. Java web 入门知识 及HTTP协议详解

     Java  web  入门知识 及HTTP协议详解 WEB入门 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资 ...

  4. Java Web入门经典扫描版

    全书共分4篇19章,其中,第一篇为“起步篇”,主要包括开启JavaWeb之门.不可不知的客户端应用技术.驾驭JavaWeb开发环境.JavaWeb开发必修课之JSP语法等内容:第二篇为“核心篇”,主要 ...

  5. Java快速入门-02-基础篇

    Java快速入门-02-基础篇 上一篇应该已经让0基础的人对 Java 有了一些了解,接一篇更进一步 使用 Eclipse 快捷键 这个老师一般都经常提,但是自己不容易记住,慢慢熟练 快捷键 快捷键作 ...

  6. Java快速入门-01-基础篇

    Java快速入门-01-基础篇 如果基础不好或者想学的很细,请参看:菜鸟教程-JAVA 本笔记适合快速学习,文章后面也会包含一些常见面试问题,记住快捷键操作,一些内容我就不转载了,直接附上链接,嘻嘻 ...

  7. java rmi 入门实例

    java rmi 入门实例 (2009-06-16 16:07:55) 转载▼ 标签: java rmi 杂谈 分类: java-基础    java rmi即java远程接口调用,实现了2台虚拟机之 ...

  8. Javaweb项目-下拉列表显示后台数据库的数据

    下面将演示前端下拉列表显示后台数据库中class表的说有班级的名称 环境: Tomcat-8.5.40 mysql-8.0.13 eclipse-4.9.0 springmvc框架 一.从mysql中 ...

  9. MariaDB——显示所有数据库列表

    显示所有数据库列表:其中,information_schema.performance_schema.test.mysql,这4个库表是数据库系统自带的表,一般不放数据. 进入某个库 切换库,并显示库 ...

随机推荐

  1. 【Java】JVM(六)虚拟机字节码执行引擎

    一.概述 执行引擎是虚拟机中最核心的部分之一, 虚拟机自己实现引擎,自己定义指令集和执行引擎的结构体系. 二.栈帧 栈帧包含(1)局部变量表.(2)操作数栈.(3)动态链接.(4)方法返回地址.(5) ...

  2. intellij idea 添加模板语句

    1.说明 在intellij idea 中有很多模板语句可有使用,比如:输入sout,就可以直接生成 System.out.println();代码, 输入psvm,就可以直接生成main方法,类似这 ...

  3. Java.sql.SQLException: 无效的列类型: 1111

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: ...

  4. django创建工程,用命令

    django创建工程的命令 >>python C:\Python33\Lib\site-packages\django\bin\django-admin.py startproject p ...

  5. Web开发: servlet的session为null?

    servlet的session(会话)显示为null,一般是web.xml中配置不对或者在浏览器输入的url不正确造成的. web.xml配置如下: <servlet> <servl ...

  6. hdoj1114 Piggy-Bank(DP 完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 思路: 题目看着有些绕,其实就是完全背包的变形,需要注意的是这里求最小值,所以需要将dp数组初始 ...

  7. windows server 2008 远程桌面连接数修改--无限连接

    1.开启远程桌面 我的电脑 |  属性 |  远程设置  |  远程 |  进允许运行使用网络级别身份验证的远程桌面的计算机连接(更安全)(N)

  8. zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 Rescue Time Limit: 2 Seconds      Mem ...

  9. 在windows系统下安装oracle 11g

     oracle 11g 安装在windows server 2012 系统下. 最近,需要配置数据库,要求在windows操作系统下,安装oracle 11g 数据库,因为以前没有安装过,所以成功后, ...

  10. 面向对象设计模式纵横谈:Builder 生成器模式(笔记记录)

    Builder模式的缘起 假设创建游戏中的一个房屋House设施,该房屋的构建由几个部分组成,且各个部分要富于变化. 如果使用最直观的设计方法,每一个房屋部分的变化,都将导致房屋构建的重新修正…… 动 ...