JSP初识
JSP:是服务器响应页面,也可以理解成是嵌入了java代码的html。JSP本质上是后端程序。
执行原理(与html作对比):
(1)jsp执行原理:
jsp --> java临时文件 -->class文件
jsp中所有的内容都会被翻译为响应流,最终执行的class文件就是响应流。也就是以响应流的形式为浏览器做出响应。
(2)html执行原理:
浏览器发出请求,访问和html文件,直接将html文件响应会浏览器,浏览器解析为html文件。
运行流程图:
字符编码设置和环境的搭建
(1)设置响应流的字符编码:
(2)设置jsp页面本身的字符编码
(3)服务器通知浏览器以什么样的字符编码来打开响应
JSP中使用base标签
页面加入basePath变量:
<% String basePath = request.getScheme() + “: “+”//” + request.getServerName() + “:” + request.getServerPort() + request.getContextPath() + “/”; %>
结合base标签使用:head下第一行引入
< base href="<%=basePath%>" >
像这样:
这样就可以方便使用,当用到要写跳转路径时 (前提是不是同一个目录下),例如用到< a href="" >标签时,举个例子。
假如当前项目名为 jsp1 ,那么basePath拿到的变量值是:
http://localhost:8080/jsp1/
然后当想要使用< a href="" >标签跳转到另一个不同文件夹下的jsp 例如/abc/test2.jsp时
没使用basePath结合base标签的情况是:
< a href="/jsp1/abc/test2.jsp">
使用basePath结合base标签的情况是:
< a href=“abc/test2.jsp”>
注:原本是:< a href="<%=basePath%>abc/test2.jsp"> 因为加入了base标签之后,就可以将basePath变量的引用省略掉;页面中所有的路径都不用加basePath变量了,路径貌似是一个相对路径(前面没加/)。同时,加入了base标签后的页面里所有的相对路径都会失效
总结:
加入了base标签之后,路径的写法统一的变成了:
所有的前端路径,都是从WebContent根下开始往下顺,前面不加 /
所有的后端(servlet).do的路径,都是 url-pattern 前面不加 /
例如:< form action=“login.do” method=“post”>
搭建模板(使得每一次建jsp文件时会自动生成basePath和base标签)
把basePath和base标签放进去就可以了。
小脚本的使用
1.<% %>
该方法中写的代码,生成在_jspService方法(在临时文件中)中,该脚本中声明的变量,为_jspService方法中的局部变量。
2.<%! %>
该方法中写的代码,生成在_jspService方法(在临时文件中)外,该脚本中声明的变量,为jsp临时文件中的成员变量。
3.<%-- --%>
针对于jsp中的注释,可以注释jsp中所有内容。通过注释形式注释掉的信息,通过查看网页源代码的方式是看不到的。
另外:在jsp中也可以使用 <!-- – >来注释html代码。该形式注释不掉java代码。通过查看网页源代码的形式可以看到。
4.<%= %>
相当于响应流的输出
out.print(); // (out是响应流声明的对象)
jsp中java脚本的拼接
通过前端html和后端java相结合,使得我们的页面展现出来的内容是动态的,就要使用到java脚本的拼接。
java脚本的拼接原则是,在使用java脚本处理业务逻辑中,遇到前端显示,则拆分java脚本。
脚本拆分练习
打印学生信息列表
Student.java
public class Student {
private String id;
private String name;
private int age;
public Student(){}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="com.domain.Student,java.util.*"
%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Student s1 = new Student("A0001","zjl",23);
Student s2 = new Student("A0002","ljj",24);
Student s3 = new Student("A0003","wlh",25);
Student s4 = new Student("A0004","lrh",26);
Student s5 = new Student("A0005","hyb",27);
List<Student> sList = new ArrayList<Student>();
sList.add(s1);
sList.add(s2);
sList.add(s3);
sList.add(s4);
sList.add(s5);
%>
<table border="1">
<tr>
<td>
序号
</td>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
操作
</td>
</tr>
<%
for(int i=0;i<sList.size();i++){
Student s = sList.get(i);
%>
<tr>
<td>
<%=i+1%>
</td>
<td>
<%=s.getId()%>
</td>
<td>
<%=s.getName()%>
</td>
<td>
<%=s.getAge()%>
</td>
<td>
修改||删除
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
结果:
JSP的指令、标签、内置对象
指令概述和语法
(1)概述: 声明jsp页面所需要的属性和动作
(2)语法: <%@指令名称 属性=“值” 属性=“值”%>
jsp指令的分类
(1)page指令:声明jsp页面的一些属性
page指令常用属性:
contentType:设置响应流的编码,及通知浏览器用什么编码打开
pageEncoding:设置页面的编码
import:导入所需要的包
(2)include指令:静态包含
包含作用,可以使一个jsp包含另一个jsp
通过指令包含,也称为静态包含
包含页面和被包含页面,只生成一份java临时脚本,包含和被包含中的变量统一生成在了同一个jspService方法中,所以变量不能重名。
(3)taglib指令:导入标签库
用来导入标签库
格式:<%@taglib prefix=“前缀名” uri=“名称空间”%>
使用指令注意事项: 一个页面可以出现多个指令,指令一般放在jsp页面最上面
jsp动作标签
(1)jsp:forward:转发标签
(2)jsp:include:包含标签
jsp动作标签中的包含是动态包含,包含文件和被包含文件分别独立生成java临时文件,不用担心变量重名问题。
jsp中内置对象
out
类型: JspWritter
request
类型:HttpServletRequest
response
类型:HttpServletResponse
session
类型:HttpSession
exception
类型:HttpSession
page
类型:Throwable
config
类型:ServletConfig
application
类型:ServletContext
pageContext
类型:PageContext
jsp的域对象
pageContext
作用范围:一个页面
request
作用范围:一次请求
session
作用范围:一次会话
application
作用范围:整个项目
最后练习
使用servlet结合jsp的方式完成学生信息管理系统
数据库
工具类
DBUtil.java
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private DBUtil(){}
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASSWORD = "root";
//取得连接
public static Connection getConn() throws SQLException{
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
return conn;
}
//关闭资源
public static void myClose(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException{
//关闭的顺序为按照 创建的顺序 逆序关闭
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
}
UUIDUtil.java
package com.util;
import java.util.UUID;
public class UUIDUtil {
public static String getUUID(){
return UUID.randomUUID().toString().replaceAll("-","");
}
}
字段类
Student.java
package com.domain;
public class Student {
private String id;
private String name;
private int age;
public Student(){}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
登录
前端页面login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>我是登录页</h3>
<form action="login.do" method="post">
账号:<input type="text" name="username"/><br/><br/>
密码:<input type="text" name="password"/><br/><br/>
<input type="submit" value="提交"/>
<a href="register.jsp">需要注册?</a>
</form>
</body>
</html>
后端servlet
LoginServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到登录操作");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select count(*) from tbl_user where username=? and password=?";
boolean flag = true;
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
/*
* 创建完ps之后,养成好习惯,观察sql语句,如果有?,需要使用ps对?进行赋值
*/
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
int count = rs.getInt(1);
/*
* count==0 没有查询出相应的账号密码 登录失败
* count==1 查询出来了一条记录 登录成功
* count>1 有垃圾数据 登录失败
*
*/
if(count!=1){
flag = false;
}
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为连jdbc,判断账号密码是否正确
* 以下为根据flag值,决定登录成功还是失败
*/
//登录成功
if(flag){
request.getSession().setAttribute("username",username);
response.sendRedirect(request.getContextPath() + "/student/list.do");
//登录失败
}else{
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
注册
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>我是注册页</h3>
<form action="register.do" method="post">
请输入账号:<input type="text" name="username"/><br/><br/>
请输入密码:<input type="text" name="password"/><br/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
后端Servlet
RegisterServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.Element;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到注册操作");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
String sql = "insert into tbl_user(username,password) values(?,?)";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
/*
* 创建完ps之后,养成好习惯,观察sql语句,如果有?,需要使用ps对?进行赋值
*/
ps.setString(1,username);
ps.setString(2,password);
count = ps.executeUpdate();
System.out.println("受到影响的行数为:" + count + "行");
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
//注册成功跳转到登录页
if(count >= 1){
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
接下来进入到下面几个操作
(注意:文件夹结构)
登录进去后的展示页
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="com.domain.Student,java.util.*"
%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<%
String username = (String)session.getAttribute("username");
List<Student> sList = (List<Student>)request.getAttribute("sList");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
td{
text-align: center;
}
a{
text-decoration: none;
}
</style>
<script type="text/javascript">
function addStu(){
window.location.href = "jsp/student/add.jsp";
}
</script>
</head>
<body>
你好:<%=username%>
<hr/>
<h3 align="center">学生信息管理系统</h3>
<table border="0" align="center" width="70%" cellspacing="0" cellpadding="6">
<tr>
<td style="text-align: left;">
<button onclick="addStu()">添加学员</button>
</td>
</tr>
</table>
<table border="1" align="center" width="70%" cellspacing="0" cellpadding="6">
<tr>
<td>
序号
</td>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
操作
</td>
</tr>
<%
for(int i=0;i<sList.size();i++){
Student s = sList.get(i);
%>
<tr>
<td>
<%=i+1%>
</td>
<td>
<%=s.getId()%>
</td>
<td>
<%=s.getName()%>
</td>
<td>
<%=s.getAge()%>
</td>
<td>
<a href="student/edit.do?id=<%=s.getId()%>">
修改
</a>
||
<a href="student/delete.do?id=<%=s.getId()%>">
删除
</a>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
后端处理数据的Servlet
StudentListServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class StudentListServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生信息列表操作");
response.setContentType("text/html;charset=utf-8");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select id,name,age from tbl_student";
List<Student> sList = new ArrayList<Student>();
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Student s = new Student();
s.setId(rs.getString(1));
s.setName(rs.getString(2));
s.setAge(rs.getInt(3));
sList.add(s);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上操作为取得了sList数据
*
* 以下为跳转到目标页面student/index.jsp
* 我们要刷新的路径是list.do,所以使用转发
*
*/
request.setAttribute("sList",sList);
request.getRequestDispatcher("/jsp/student/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
添加
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>我是学生信息添加页</h3>
<form action="student/save.do" method="post">
请输入姓名:<input type="text" name="name"/><br/><br/>
请输入年龄:<input type="text" name="age"/><br/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
后端Servlet
StudentSaveServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
import com.util.UUIDUtil;
public class StudentSaveServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生添加操作");
//接收表单信息
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
int age = Integer.valueOf(ageStr);
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into tbl_student(id,name,age) values(?,?,?)";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1, UUIDUtil.getUUID());
ps.setString(2, name);
ps.setInt(3, age);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为完成业务逻辑,执行了学生信息的添加操作
*
* 之后跳转到学生信息列表页 student/list.do
* 登录成功后,添加修改删除统一重定向到student/list.do
* 重定向:
* list.do
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
修改
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="com.domain.Student"
%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<%
Student s = (Student)request.getAttribute("s");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>我是学生信息修改页</h3>
<form action="student/update.do" method="post">
<input type="hidden" name="id" value="<%=s.getId()%>"/>
请编辑姓名:<input type="text" name="name" value="<%=s.getName()%>"/><br/><br/>
请编辑年龄:<input type="text" name="age" value="<%=s.getAge()%>"/><br/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
后端Servlet
StudentEditServlet.java (处理选中的单条记录,进行跳转到修改页操作)
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class StudentEditServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到根据id查单条 跳转到修改页操作");
//接收需要修改记录的id
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select name,age from tbl_student where id=?";
Student s = new Student();
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1,id);
rs = ps.executeQuery();
if(rs.next()){
s.setId(id);
s.setName(rs.getString(1));
s.setAge(rs.getInt(2));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
request.setAttribute("s",s);
request.getRequestDispatcher("/jsp/student/edit.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
StudentUpdateServlet.java (处理修改后,跳转并刷新展示页的操作)
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
import com.util.UUIDUtil;
public class StudentUpdateServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生修改操作");
//接收表单信息
String id = request.getParameter("id");
String name = request.getParameter("name");
String ageStr = request.getParameter("age");
int age = Integer.valueOf(ageStr);
Connection conn = null;
PreparedStatement ps = null;
String sql = "update tbl_student set name=?,age=? where id=?";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,id);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
*
* 登录成功后,添加修改删除后,统一重定向到student/list.do
*
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
删除
StudentDeleteServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.domain.Student;
import com.util.DBUtil;
public class StudentDeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入到学生删除操作");
//接收需要删除记录的id
String id = request.getParameter("id");
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from tbl_student where id=?";
try{
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql);
ps.setString(1,id);
ps.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
DBUtil.myClose(conn, ps, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* 以上为完成业务逻辑,执行了学生信息的删除操作
*
* 之后跳转到学生信息列表页 student/list.do
* 重定向:
* list.do
*
*/
response.sendRedirect(request.getContextPath() + "/student/list.do");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>jsp1</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>com.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentList</servlet-name>
<servlet-class>com.servlet.StudentListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentList</servlet-name>
<url-pattern>/student/list.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentSave</servlet-name>
<servlet-class>com.servlet.StudentSaveServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentSave</servlet-name>
<url-pattern>/student/save.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentEdit</servlet-name>
<servlet-class>com.servlet.StudentEditServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentEdit</servlet-name>
<url-pattern>/student/edit.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>studentUpdate</servlet-name>
<servlet-class>com.servlet.StudentUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentUpdate</servlet-name>
<url-pattern>/student/update.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deleteList</servlet-name>
<servlet-class>com.servlet.StudentDeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deleteList</servlet-name>
<url-pattern>/student/delete.do</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RegisterServlet</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
</web-app>
结束语
交由你来写如何?