webApp结构
注意
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1 align="center"><a href="/ServletWebAppJDBC/system/list">显示emp表中的数据</a></h1>
</body>
</html>
web.xml编写规范
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<servlet>
<servlet-name>listEmp</servlet-name>
<servlet-class>com.FirstServletWebApp.ListEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>listEmp</servlet-name>
<url-pattern>/system/list</url-pattern>
</servlet-mapping>
</web-app>
ListEmpServlet.java文件
package com.FirstServletWebApp;
import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class ListEmpServlet implements Servlet
{
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
servletResponse.setContentType("text/html;charset=UTF-8");
PrintWriter out=servletResponse.getWriter();
out.print(" <!doctype html> ");
out.print(" <html lang='en'> ");
out.print(" <head> ");
out.print(" <meta charset='UTF-8'> ");
out.print(" <title>员工信息</title> ");
out.print(" </head> ");
out.print(" <body> ");
out.print(" <h3 align='center'>员工列表emp</h3> ");
out.print(" <hr width='60%'> ");
out.print(" <table border='1' align='center' width='50%'> ");
out.print(" <tr align='center'> ");
out.print(" <th>序号</th> ");
out.print(" <th>员工编号</th> ");
out.print(" <th>员工姓名</th> ");
out.print(" <th>员工薪水</th> ");
out.print(" </tr> ");
//JDBC
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/company","root","105105");
String sql="select empno,ename,sal from emp";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
int i=0;
while(rs.next()){
String empno=rs.getString("empno");
String ename=rs.getString("ename");
String sal=rs.getString("sal");
out.print(" <tr align='center'> ");
out.print(" <th>"+(++i)+"</th> ");
out.print(" <th>"+empno+"</th> ");
out.print(" <th>"+ename+"</th> ");
out.print(" <th>"+sal+"</th> ");
out.print(" </tr> ");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}if (ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
out.print(" </table> ");
out.print(" </body> ");
out.print(" </html> ");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
运行输入http://localhost:8080/ServletWebAppJDBC/index.html
点击