-
首先完成数据库的书写:
数据库的创建,表的创建和数据的插入
#判断存在即删除数据库
drop database if exists myweb;
#创建数据库
create database myweb;
#使用数据库
use myweb;
#创建表
create table user
(
uid int primary key auto_increment,
name varchar(12),
password varchar(12),
phone varchar(12),
address varchar(24)
);
#插入数据
INSERT into user (name,password,phone,address) VALUES ('小鱼干','123456','12345678910','晨岛 预言山谷');
INSERT into user (name,password,phone,address) VALUES ('大薯条','001218','23665456565','禁阁 星光沙漠');
INSERT into user (name,password,phone,address) VALUES ('花花','111222','46455244564','云野 圣岛');
INSERT into user (name,password,phone,address) VALUES ('园子','223344','14343545435','雨林 大树屋');
CREATE table fly(
id int PRIMARY key auto_increment,
flyname varchar (12),
flyPrice DOUBLE ,
mark varchar (32)
);
insert into fly(flyname,flyPrice,mark) VALUES ("福娃斗篷",168,"红脸蛋+福娃发型+福娃斗篷");
insert into fly(flyname,flyPrice,mark) VALUES ("瑞雪斗篷",98,"蓝色毛球斗篷");
insert into fly(flyname,flyPrice,mark) VALUES ("蝙蝠斗篷",198,"南瓜头+蝙蝠斗篷");
insert into fly(flyname,flyPrice,mark) VALUES ("欧若拉斗篷",168,"金色小翅膀伪无翼");
insert into fly(flyname,flyPrice,mark) VALUES ("小鱼斗篷",128,"小鱼头饰+小鱼斗篷");
insert into fly(flyname,flyPrice,mark) VALUES ("TGC斗篷",198,"黑底天蓝色斗篷");
第二步:完成java类的书写:
UserDao包中书写登录和注册时对数据库的操作
Servlet包中是不同的映射对应的不同的Servlet类
jdbc工具类的书写为了简化代码
用户信息类和查找出来的内容的类
package com;
public class User {
private Integer id;
private String name;
private String password;
private String phone;
private String adress;
public User() {
}
public User(Integer id, String name, String password, String phone, String adress) {
this.id = id;
this.name = name;
this.password = password;
this.phone = phone;
this.adress = adress;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", adress='" + adress + '\'' +
'}';
}
}
package com;
public class Fly {
private Integer id;
private String flyname;
private double flyPric;
private String mark;
public Fly() {
}
public Fly(Integer id, String flyname, double flyPric, String mark) {
this.id = id;
this.flyname = flyname;
this.flyPric = flyPric;
this.mark = mark;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFlyname() {
return flyname;
}
public void setFlyname(String flyname) {
this.flyname = flyname;
}
public double getFlyPric() {
return flyPric;
}
public void setFlyPric(double flyPric) {
this.flyPric = flyPric;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
@Override
public String toString() {
return "Fly{" +
"id=" + id +
", flyname='" + flyname + '\'' +
", flyPric='" + flyPric + '\'' +
", mark='" + mark + '\'' +
'}';
}
}
第三步完成jsp页面的书写:
- 首页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<%-- 添加背景图片--%>
<body style="margin: 200px auto;background-image:url(img/1.png);background-size: 100% 100%">
<div align="center" style="width: 100px; height: 100px; margin: 0px auto ">
<a href="dl.jsp" style="font-size: 30px ;color: azure;"> 登录</a></br>
<a href="zc.jsp" style="font-size: 30px;color: azure">注册</a>
</div>
</body>
</html>
- 登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>sky 登录页面</title>
</head>
<body style="background-color: lightblue">
<div style="width: 200px;height: 300px;margin:200px auto;padding-left: 20px">
<form action="dl" method="post" >
登录账号:<input type="text" name="name" value=""></br>
登录密码:<input type="text" name="password" value=""></br>
<input type="submit" value="提交">
<a href="zc.jsp">没有账号?去注册</a>
</form>
</div>
</body>
</html>
- 注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="zhuce" method="post">
登录账号:<input type="text" name="name" value=""></br>
密码:<input type="text" name="password" value=""></br>
确认 密码:<input type="text" name="passwordToo" value=""></br>
电话:<input type="text" name="phone" value=""></br>
光遇出生地:<input type="text" name="adress" value=""></br>
<input type="submit" value="提交"><a href="dl.jsp">有账号?去登录</a>
</form>
</body>
</html>
- 登录成功主页:
在主页中运用了
1. El表达式 :${ } 从作用域中获取数据,如果该数据是对象,则可以通过方法名.属性名获取其数值
2. JSTL标签库:
(1)导入JSTL jar包 (核心类库) <%@taglib prefix="起的别名" uri="位置" %>
(2) 使用forEach循环遍历从后端传来的数据:
<%@taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body style="background-image:url(img/zy.jpg);background-size: 100% 100%">
<h2 align="center">欢迎${user.name}带上耳机,感受更真实</h2>
<form>
<a href="inserte.jsp" style="margin-left: 50px">添加商品</a>
<table style="color: azure ;margin: 100px auto;font-size: 20px">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>介绍</th>
<th></th>
<th></th>
</tr>
<a:forEach items="${flydao}" var="aa">
<tr>
<td>${aa.id}</td>
<td>${aa.flyname}</td>
<td>${aa.flyPric}</td>
<td>${aa.mark}</td>
<td><a href="Update?id=${aa.id}">修改</a></td>
<td><a href="delete?id=${aa.id}">删除</a></td>
</tr>
</a:forEach>
<td><a>修改</a></td>
<td><a>删除</a></td>
</table>
</form>
</body>
</html>
- 登录错误页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>登录错误</h3>
</body>
</html>
第四步:完成jdbc的工具类的书写
简化代码,static修饰方法不用实例化就可以直接进行调用,关闭资源的代码使用方法的重载。
package utill;
import java.sql.*;
public class JDBC {
public static Connection webJdbc () throws ClassNotFoundException, SQLException {
Connection con = null;
String url = "jdbc:mysql://127.0.0.1:3306/sky";
String username = "root";
String pass = "root";
// 加载jdbc驱动jar包
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
con = DriverManager.getConnection(url,username,pass);
return con;
}
// 资源的关闭方法和下面的方法时方法的重载
public static void close(Connection con,PreparedStatement ps,ResultSet rs){
try{
if(con!=null){
con.close();
}
if(ps!=null){
con.close();
}
if(rs!=null){
con.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
// 方法名相同参数列表不同
public static void close(Connection con,PreparedStatement ps){
try{
if(con!=null){
con.close();
}
if(ps!=null){
con.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
第五步完成用户信息和展示数据所需的数据库操作的书写:
用户:
package dao.iml;
import com.Fly;
import com.User;
import dao.Useriml;
import utill.JDBC;
import javax.servlet.annotation.WebServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao implements Useriml {
@Override
public User denglu(String name, String password) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT * from user where name = ? and password = ?";
User user = null;
try {
con = JDBC.webJdbc();
ps = con.prepareStatement(sql);
ps.setObject(1,name);
ps.setObject(2,password);
rs = ps.executeQuery();
if (rs.next()){
user = new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
user.setPhone(rs.getString(4));
user.setAdress(rs.getString(5));
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBC.close(con,ps,rs);
}
return user;
}
@Override
public int zhuce(User user) {
// 声明变量
Connection con = null;
PreparedStatement ps = null;
int i =0;
// 准备sql语句
String sql = "INSERT into user (name,password,phone,adress) VALUES (?,?,?,?)";
try{
// 加载驱动程序,连接数据库
con = JDBC.webJdbc();
// 对sql语句进行预处理
ps = con.prepareStatement(sql);
ps.setObject(1,user.getName());
ps.setObject(2,user.getPassword());
ps.setObject(3,user.getPhone());
ps.setObject(4,user.getAdress());
System.out.println();
// 获取改变的行数
i = ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
// 关闭资源
JDBC.close(con,ps);
}
return i;
}
@Override
public int update(Fly fly) {
System.out.println("执行了userdao");
// 声明变量
Connection con = null;
PreparedStatement ps = null;
int i =0;
// 准备sql语句
String sql = "INSERT into fly (flyname,flyPrice,mark) VALUES (?,?,?)";
try{
// 加载驱动程序,连接数据库
con = JDBC.webJdbc();
// 对sql语句进行预处理
ps = con.prepareStatement(sql);
ps.setObject(1,fly.getFlyname());
ps.setObject(2,fly.getFlyPric());
ps.setObject(3,fly.getMark());
// 获取改变的行数
i = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBC.close(con,ps);
}
return i;
}
}
展示信息:
package dao.iml;
import com.Fly;
import com.User;
import jdk.nashorn.internal.scripts.JD;
import utill.JDBC;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class FlyDao {
//书写方法对主页需要的数据尽心工属具库的搜索
public List<Fly> flydao (){
// 声明一个List对象
List<Fly> flyList = new ArrayList<>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 准备sql语句
String sql = "SELECT * from fly ";
// 声明user对象
User user = null;
try {
// 导入jar包 练级数据库
con = JDBC.webJdbc();
// 对sql语句进行预处理
ps = con.prepareStatement(sql);
// 运行sql并收集结果
rs = ps.executeQuery();
// 循环遍历list集合
while (rs.next()){
// 每循环的一次都声明一个Fly对象
Fly fly = new Fly();
// 将数据都放入Fly
fly.setId(rs.getInt(1));
fly.setFlyname(rs.getString(2));
fly.setFlyPric(rs.getDouble(3));
fly.setMark(rs.getString(4));
flyList.add(fly);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBC.close(con,ps,rs);
}
return flyList;
}
//删除数据
public int delect(int id) {
System.out.println("执行了userdao");//非必须代码,可以在控制台中
// 声明变量
Connection con = null;
PreparedStatement ps = null;
int i =0;
// 准备sql语句
String sql = "delete from fly where id =?";
try{
// 加载驱动程序,连接数据库
con = JDBC.webJdbc();
// 对sql语句进行预处理
ps = con.prepareStatement(sql);
ps.setObject(1,id);
// 获取改变的行数
i = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBC.close(con,ps);
}
return i;
}
//根据id查询单条数据
public Fly select(int i) {
System.out.println("执行select");
Connection con =null;
PreparedStatement ps = null;
ResultSet rs = null;
Fly fly = null;
try {
con = JDBC.webJdbc();
String sql = "select * from fly where id =?";
ps =con.prepareStatement(sql);
ps.setObject(1,i);
rs =ps.executeQuery();
fly =new Fly();
if(rs.next()){
fly.setId(rs.getInt("id"));
fly.setFlyname(rs.getString("flyname"));
fly.setFlyPric(rs.getDouble("flyPrice"));
fly.setMark(rs.getString("mark"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
JDBC.close(con,ps,rs);
}
return fly;
}
//修改数据
public int update(Fly fly){
System.out.println("执行了update");
// 声明变量
Connection con = null;
PreparedStatement ps = null;
int i =0;
// 准备sql语句
String sql = "update fly set flyname =? ,flyPrice = ?, mark = ? where id = ?";
try{
// 加载驱动程序,连接数据库
con = JDBC.webJdbc();
// 对sql语句进行预处理
ps = con.prepareStatement(sql);
ps.setObject(1,fly.getFlyname());
ps.setObject(2,fly.getFlyPric());
ps.setObject(3,fly.getMark());
ps.setObject(4,fly.getId());
// 获取改变的行数
i = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBC.close(con,ps);
}
return i;
}
}
第六步完成Servlet的书写:
javaWeb中的四大域对象:
在JavaWeb中一共四个域对象,其servlet中可以使用的是request、session、application三个对象,而在JSP中可以使用pageContext、request、session、application四个域对象
- PageContext 页面间作用域
当前jsp页面范围内有效,它的范围也是四个域对象中最小的!
- requset 请求间作用域
一次请求内有效,范围大于PageContext
- session 会话间作用域
一整个会话中都有效(打开浏览器到浏览器关闭)
- application 服务器级作用域
整个web工程范围内都有效(web工程不停止,数据都在)
四打与对象的范围 PageContext < requset < session <application
servlet 中重定向和请求转发的区别:
重定向:是服务器端接收到客户端的请求之后,会给客户端返回了一个临时响应头,这个临时响应头中记录了,客户端需要再次发送请求(重定向)的 URL 地址,客户端再收到了地址之后,会将请求发送到新的地址上,这就是请求重定向。
response.sendRedirect("zhuye.jsp");
请求转发:发生在服务端程序内部,当服务器端收到一个客户端的请求之后,会先将请求,转发给目标地址,再将目标地址返回的结果转发给客户端。
request.getRequestDispatcher("flyDao").forward(request,response);
登录:
package Servlet;
import com.User;
import dao.Useriml;
import dao.iml.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class Skydl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
// 从前端页面获取数据
String name = req.getParameter("name");
String password = req.getParameter("password");
// 实例化UserDao对象
UserDao userdao = new UserDao();
// 调用userdao对象方法
User user = userdao.denglu(name,password);
// 进行判断
if (user!=null) {
// 获取session对象
HttpSession session = req.getSession();
// 将用户信息放入session对象中
session.setAttribute("user",user);
// 执行查询操作=(刷新页面)
req.getRequestDispatcher("flyDao").forward(req,resp);
} else {
resp.sendRedirect("eirr.jsp");
}
}
}
注册:
package Servlet;
import com.User;
import dao.iml.UserDao;
import utill.JDBC;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.JdbcRowSet;
import java.io.IOException;
import java.sql.*;
public class zhuce extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 解决中文乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
// 获取前端页面传递过来的数据
String name = req.getParameter("name");
String password = req.getParameter("password");
String phone = req.getParameter("phone");
String adress = req.getParameter("adress");
//实例化User对象
User user= new User();
//将数据放入User对象中
user.setName(name);
user.setPassword(name);
user.setPhone(name);
user.setAdress(name);
Connection con = null;
int i =0;
try {
// 加载驱动和连接数据库(调用自己写的工具类方法)
con = JDBC.webJdbc();
// 实例化对象
UserDao userdao = new UserDao();
// 调用zhuce方法
i =userdao.zhuce(user);
} catch (Exception e) {
e.printStackTrace();
}
if (i>0){
System.out.println(i);
resp.sendRedirect("dl.jsp");
}else {
System.out.println(i);
resp.sendRedirect("eirr.jsp");
}
}
}
对主页的数据进行展示:
package Servlet;
import com.Fly;
import dao.iml.FlyDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/flyDao")
public class servletFly extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取flyDao对象
FlyDao flyDao = new FlyDao();
// 调用该方法
List<Fly> flydao = flyDao.flydao();
// 控制台打印
System.out.println(flydao);
// 获取session对象
HttpSession session = req.getSession();
// 放入从数据库中获取的数据
session.setAttribute("flydao",flydao);
// 跳转页面
resp.sendRedirect("zhuye.jsp");
}
}
添加数据:
package Servlet;
import com.Fly;
import dao.iml.UserDao;
import utill.JDBC;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
@WebServlet("/fly")
public class AddFly extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了fly");
// 解决中文乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
// 获取前端页面传递过来的数据
String name = req.getParameter("name");
String price = req.getParameter("price");
String mark = req.getParameter("mark");
Fly fly = new Fly();
fly.setFlyname(name);
fly.setFlyPric(Double.parseDouble(price));
fly.setMark(mark);
Connection con = null;
int i =0;
try {
con = JDBC.webJdbc();
UserDao userdao = new UserDao();
i =userdao.update(fly);
} catch (Exception e) {
e.printStackTrace();
}
if (i>0){
System.out.println(i);
req.getRequestDispatcher("flyDao").forward(req,resp);
}else {
System.out.println(i);
resp.sendRedirect("eirr.jsp");
}
}
}
删除数据:
package Servlet;
import com.Fly;
import dao.iml.FlyDao;
import dao.iml.UserDao;
import utill.JDBC;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
@WebServlet("/delete")
public class delectFly extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了delectFly");
// 解决中文乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
String str = req.getParameter("id");
System.out.println(str);
int id =Integer.parseInt(req.getParameter("id"));
int i =0;
FlyDao flyDao = new FlyDao();
i =flyDao.delect(id);
if (i>0){
System.out.println(i);
req.getRequestDispatcher("flyDao").forward(req,resp);
}else {
System.out.println(i);
resp.sendRedirect("eirr.jsp");
}
}
}
修改:(分两步,首先通过主页点击修改按钮获取点前点击的数的id值,显示在下一个页面后进行修改)
1.
package Servlet;
import com.Fly;
import dao.iml.FlyDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
@WebServlet("/Update")
public class ByIdFly extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int i =Integer.parseInt(req.getParameter("id"));
FlyDao flydao = new FlyDao();
Fly fly = flydao.select(i);
System.out.println(fly);
if (fly!=null){
req.setAttribute("fly",fly);
req.getRequestDispatcher("Update.jsp").forward(req,resp);
}else{
req.getRequestDispatcher("eirr.jsp");
}
}
}
2.
package Servlet;
import com.Fly;
import dao.iml.FlyDao;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/up")
public class UpdateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
// 声明一个fly对象
Fly fly = new Fly();
// 将数据从主页面获取的数据存放入fly对象中
fly.setId(Integer.parseInt(request.getParameter("id")));
fly.setFlyname(request.getParameter("name"));
fly.setFlyPric(Double.parseDouble(request.getParameter("price")));
fly.setMark(request.getParameter("mark"));
FlyDao flyDao = new FlyDao();
int i = flyDao.update(fly);
// 判断受影响的行数(即i)大于0(修改成功)
if (i>0){
request.getRequestDispatcher("flyDao").forward(request,response);
}else{
response.sendRedirect("eirr.jsp");
}
}
}
第六步书写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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>denglu</servlet-name>
<servlet-class>Servlet.Skydl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>denglu</servlet-name>
<url-pattern>/dl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>zhuce</servlet-name>
<servlet-class>Servlet.zhuce</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>zhuce</servlet-name>
<url-pattern>/zhuce</url-pattern>
</servlet-mapping>
</web-app>
xml中的映射也可以写在Servlet类上方,例:
地址映射为 /delect 时将会执行当前的servlet类
运行代码