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就需要看一下后台数据表格的结构:

就是这些!!

  

原文地址:https://www.cnblogs.com/zhangshitong/p/4870867.html