1.需要在web.xml文件中写配置
去找到这个资源web.xml
package com.javacoffee.test; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class Test extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("这是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起一个名字,这个名字要和一个标签servlet-mapping对应--> <servlet-name>Test</servlet-name> <!-- servlet-class是servle所对应的类--> <servlet-class>com.javacoffee.test.Test</servlet-class> <!-- 设置为1是当tomcat服务器已启动就会加载这个类--> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern 找到这个资源的入门,类似于咱们写的@WebServlet("/webxml") --> <!-- 可以写多个url-pattern 相当于给资源起了很多的名字 但是请求只有一个<servlet-name> Test --> <servlet-mapping> <servlet-name>Test</servlet-name> <url-pattern>/Test</url-pattern> </servlet-mapping> </web-app>
这种写只能是Servlet版本是3.0以上的版本才可以使用注解,
现在咱们都是3.0以上的了。所以以后用注解的写法。从这地方可以说明一个问题
去xml的写法,xml太麻烦了。咱们ssm框架 大部分都是xml,后来出来一个springboot
把xml省去了,加了好多的注解。
package com.javacoffee.test; 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; @WebServlet("/Test") public class Test extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("这是XML注解写法"); } }
Servlet在执行的时候,经历过这些过程:
1.construct
2.init
3.service
4.destory
阶段1:
Servlet这个类在Tomcat中进行了实例化,提供给Tomcat使用
阶段2:
Servlet的初始化
阶段3:
Servlet对外提供服务,相当于doGet和doPost方法
阶段4:
servlet进行销毁,可以手动销毁,调用destroy方法,咱们一般不用调用,tomcat只要关闭就会销毁Servlet
想要学好Servlet必须紧紧围绕着两个东西
请求和响应这两个概念。
问大家一个问题?咱们现在写的代码只是进行请求,然后再响应到客户端。
请求的时候带数据给Servlet了没?没有带数据给Servlet
现在咱们要讲在请求的时候前端带数据到Servlet里面。咱们Servlet要接受前端给我的这个数据
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="TestServlet" method="get"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="password"><br> <input type="submit" name="提交"> </form> </body> </html>
package com.javacoffee.servlet; 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; @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //前端发送数据到我这servlet了,我这个Servlet能不能接一些数据? //前端发送求过来,说明和request 对象 //http:localhost:8080/login.html?user=gouda&pwd=123456 //这个getParameter方法就是获取请求对象的参数的,通过 //前端input标签name的属性值获取的 //解决post请求时候的乱码 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); //在TestServlet界面输出 response.getWriter().println("姓名:"+username+",密码:"+password); } }
web网站上面有一些跳转按钮。比如 登录成功以后跳转到主页面!!!
用户通过浏览器发送一个请求,Tomcat服务器接收这个请求,会给浏览器发送一个状态码302
并设置一个重定向的路径,浏览器如果接收到,了这个302的状态码以后,就会去自动加载服务器设置的路径。
一个页面跳转到另外一个页面(场景)
登录页面跳转到主页
denglu.html===>TestLoginServlet=>target.html
特征:
1.重定向的过程是浏览器(客户端)的行为
2.实际上浏览器做了几次请求 (当点击登录按钮的时候做了两次请求)(TestServlet,loginSeccess.html)
3.注意上一次请求的request对象会丢失
4.重定向有一个非常明显的特征,浏览器的url变化了
5.重定向可以重定向到网络的任意资源(比如京东淘宝等网络资源)
response.sendRedirect("target.html");就这一行代码,但是这一行代码必须写在doget或者dopost
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="TestServlet" method="post"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="password"><br> <input type="submit" name="提交"> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆成功页面</title> </head> <body> <div style="color: red; font-size: 30px">这是重定向跳转的页面!</div> </body> </html>
接着是TestServlet类
package com.javacoffee.servlet; 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; @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //前端发送数据到我这servlet了,我这个Servlet能不能接一些数据? //前端发送求过来,说明和request 对象 //http:localhost:8080/login.html?user=gouda&pwd=123456 //这个getParameter方法就是获取请求对象的参数的,通过 //前端input标签name的属性值获取的 //解决post请求时候的乱码 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //登录以后跳转到主页 //重定向 //这个TestServlet里面的数据是不能传给loginSeccess.html response.sendRedirect("loginSeccess.html"); } }
用户发送请求到服务器
服务器接收当前请求
会调用内部方式(转发)处理该请求
最终把响应给客户端
特征:
1.转发是服务器的行为
2.浏览器在那个过程中只有一次行为
3.转发可以带有数据 request对象中
4.url不会发生任何变化的
5.转发只能在当前项目中进行转发,不能转发外部的资源
package com.javacoffee.servlet; 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; @WebServlet("/TestServlet") public class TestServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); //给当前的request对象设置一些数据,request对象带有数据 request.setAttribute("username",username); /* 可以通过request对象转发到其他资源(另外一个Servlet)里面 * */ //转发会将一个Servlet里面数据带到另外一个servlet里面 request.getRequestDispatcher("TestServlet1").forward(request,response); } }
package com.javacoffee.servlet; 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; @WebServlet("/TestServlet1") public class TestServlet1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //接受从TestServlet传过来的数据 String username1 = (String) request.getAttribute("username"); //在界面显示出来 response.getWriter().append(username1); } }
简单的注册登录删除修改页面
注册,存放数据到数据库中
点击登录按钮(判断前端传给我的数据在数据库中是否存在)(通过查询来进行)
如果用户名和密码都是正确我才让你登录,转发到主页面
转发到主页
然后可以进行增删
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册界面</title> <style> body{ background-color: pink; } div{ position: absolute; top: 10px; left: 500px; width: 500px; height: 500px; background-color:darkkhaki; } </style> </head> <body> <div> <p style="font-size: 50px;color: red">欢迎来到注册界面!</p> <form action="RegServlet" method="post"> 请输入用户名:<input type="text" name="username" id="input1"><br> 请输入密码:<input type="password" name="password" id="input2"><br> <input type="submit" name="确定注册" style="width: 100px"> <input type="reset" name="清空" style="width: 100px"> <br> </form> </div> </body> </html>
<%-- Created by IntelliJ IDEA. User: 86156 Date: 2021/9/10 Time: 22:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆成功页面</title> </head> <body> <div style="background-color: pink;font-size: 80px">恭喜<%=request.getAttribute("username")%>注册成功!!!</div> <form action="Login.html" method="get"> <input type="submit" name="去登陆" value="去登陆!" style="width: 100px;height: 30px"> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> <style> body{ background-color: pink; } div{ position: absolute; top: 10px; left: 500px; } </style> </head> <body> <div> <p style="font-size: 50px;color: red;background-color: pink">欢迎来到登录界面!</p> <form action="LoginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="pwd"><br> <input type="submit" name="登录"> </form> </div> </body> </html>
<%-- Created by IntelliJ IDEA. User: 86156 Date: 2021/9/10 Time: 22:09 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆成功</title> </head> <body> <div > <p style="font-size: 50px">删除用户</p> <form action="DeleteServlet" method="post"> 要删除的用户的用户名 <input type="text" name="username"><br> <input type="submit" name="删除" value="删除!" style="width: 100px"> </form> </div> <br><br><br> <div > <p style="font-size: 50px">修改用户</p> <form action="UpdateServlet" method="post"> 要修改的用户的用户名 <input type="text" name="lastUsername"><br> 新用户名: <input type="text" name="newUsername"><br> 新密码: <input type="password" name="newPassword"><br> <input type="submit" name="修改" value="修改!" style="width: 100px"> </form> </div> </body> </html>
<%-- Created by IntelliJ IDEA. User: 86156 Date: 2021/9/10 Time: 22:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>删除成功</title> </head> <body> <form action="LoginSuccess.jsp" method="get"> <input type="submit" name="返回" value="返回" style="width: 100px;height: 50px"> </form> </body> </html>
<%-- Created by IntelliJ IDEA. User: 86156 Date: 2021/9/10 Time: 22:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改成功</title> </head> <body> <form action="LoginSuccess.jsp" method="get"> <input type="submit" name="返回" value="返回" style="width: 100px;height: 50px"> </form> </body> </html>
util层用了自己封装的JdbcUtil BaseDao类获取增删改查操作
package com.javacoffee.util; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.io.FileInputStream; import java.io.IOException; import java.sql.*; import java.util.Properties; /** * 1.完成驱动的自动加载 * 2.完成必要的数据处理 url user password * 3.完成connection这个方法 * 4.完成统一的close方法 */ public class JdbcUtil { //为什么呢么是static修饰的?是因为getConnection是静态方法, //静态方法中不能使用非静态的后成员属性 private static ComboPooledDataSource pool = new ComboPooledDataSource(); //简化getConnection方法的写法 //封装一个静态的方法,获取connetion对象 public static Connection getConnection() { Connection connection = null; try { connection = pool.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return connection; } //现在封装的时候,我不知道需要关闭哪个资源 //有的需要关闭一个,有的需要关闭两个,有的需要关闭三个 //关闭一个资源 connection public static void close(Connection connection) { close(connection, null, null); } //关闭两个资源 connection statement public static void close(Connection connection, Statement statement) { close(connection, statement, null); } //关闭三个资源的 connection statement resultSet public static void close(Connection connection, Statement statement, ResultSet resultSet) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package com.javacoffee.util; import org.apache.commons.beanutils.BeanUtils; import java.lang.reflect.InvocationTargetException; import java.sql.*; import java.util.ArrayList; import java.util.List; public class BaseDao { /** * 完成一个统一的增删改的方法 * 方法名字叫 update * 方法需要参数: * 1.String sql * 2.需要sql语句对应的参数 * 参数不确定的,因为你不知道你写的sql语句,具体是需要几个参数 * 放一个数组进来,Object[] 因为Object可以存放不同类型的数据 * 3.返回值 * int 受影响的行数 * */ public int update(String sql, Object[] parameters) throws SQLException { if (sql == null) { throw new SQLException("sql is null"); } //1.获取数据库的连接 Connection connection = JdbcUtil.getConnection(); //2.获取数据库预处理搬运工对象 PreparedStatement preparedStatement = connection.prepareStatement(sql); //3.现在我不知道这个sql语句有几个? //3.获取参数元数据对象,获取参数的个数 //insert into work(name, age,info) values(?,?,?) int parameterCount = preparedStatement.getParameterMetaData().getParameterCount(); //4.对sql里面的?进行赋值 三个? setObject 三次 两个? setObject两个 for (int i = 1; i <= parameterCount; i++) { /* * String sql = "insert into work(name, age, info) values(?,?,?)"; Object[] objs = {"杨仔", 78, "年出生的警察局"}; * * */ // preparedStatement.setObject(1,"杨仔"); // preparedStatement.setObject(2,78); // preparedStatement.setObject(3,"出生的警察局"); preparedStatement.setObject(i, parameters[i-1]); } //5.执行sql语句 int i = preparedStatement.executeUpdate(); //6.关闭资源 JdbcUtil.close(connection, preparedStatement); //7.返回受影响的行数 return i; } /** * 完成统一的查询方法 * 1、查询一条数据可以 * 2、查询多条数据可以 * 3.查询任意类型的数据 * 方法的分析: * 方法名字: query * 方法的参数: * 1.sql * 2.sql占位符,使用的Object数组 * 3.需要第三个参数 * 大家想一个问题,比如查询的是work表,要把work‘这个表数据赋值给Work实体类 * 再查询person表,咱们把person表中数据赋值给Person类。赋值的实体类固定吗? * 让他具有普适性,需要的是一个对象。如果是Object需要强转,怕出错 * 用反射!!! 使用的是Class<T> * 方法的返回值: * List<T> * * */ public <T> List<T> query(String sql, Object[] parameters, Class<T> cls) throws SQLException { if (sql == null || cls == null) { throw new NullPointerException(); } //1.获取connection对象 Connection connection = JdbcUtil.getConnection(); //2.预处理sql语句 select* from work where id = ? PreparedStatement preparedStatement = connection.prepareStatement(sql); //3.处理sql语句里面的? 获取参数的个数 int parameterCount = preparedStatement.getParameterMetaData().getParameterCount(); //4.给?进行赋值 //什么时候执行这个for循环进行setObject呢? //select * from work where id = ? //Objects[] objs = {10,20} //写健壮一点,来一些判断 数组中的个数和sql参数是否保持一致 //是因为数组中的值刚好赋值给sql的? if(parameters != null && parameterCount == parameters.length) { for (int i = 1; i <= parameterCount; i++) { preparedStatement.setObject(i, parameters[i - 1]); } } //5.执行sql语句,和增删改 不一样之处,因为查询的时候返回的是一个ResultSet对象 ResultSet resultSet = preparedStatement.executeQuery(); //6.准备一个List集合 ,目的是放对象的,把多个对象或者一个对象放到这个集合中 //有一个问题?你知道是哪个对象吗? ArrayList<T> ts = new ArrayList<>(); //7.获取结果集元数据对象,获取字段的名字,还有列数(字段的个数),去获取数据表中的行数据 ResultSetMetaData metaData = resultSet.getMetaData(); //8.获取字段的个数 int columnCount = metaData.getColumnCount(); //9.遍历数据 while (resultSet.next()) {//控制行数 //10.通过Class类对象获取某一个类的对象 Work.class // Person.class //Teacher.class // 就是一个Class类对象 // Work work = new Work(); T t = null;//t相当于new 的对象 比如你传的是Work.class //这个t new的work对象 //比如你传的是Person.class 这个t就是 new的person对象 try { t = cls.getConstructor(null).newInstance(null); for (int i = 1; i <= columnCount; i++) {//控制的列数 //比如 id name age info 这些字段是通过for循环获取出来的 //11.获取字段的名字,通过结果集获取数据库的名字 //当i=1 获取 id字段 i=2 获取name字段名字 ...... String columnName = metaData.getColumnName(i);//字段的名字 //12.通过字段获取对应的值 Object value = resultSet.getObject(columnName); //13.将值存到对象中,将对象存到集合中,但是现在没有对象 //new Work();//相当于把他写死了,封装,普适性。 //t.setId()//现在呢开始对对象进行赋值?咋办? //columnName 字段的名字,所以规定数据表中字段的名字一定要和 //实体类中 成员属性一致。 // //BeanUtils.setProperty(work, id, 1); //BeanUtils.setProperty(work, name, "狗蛋"); BeanUtils.setProperty(t, columnName, value); } } catch (Exception e) { e.printStackTrace(); } //第一个的while循环对t 对象 进行一次赋值 第二次while循环对t再进行一次赋值 //14.将这个t存到list集合中 ts.add(t); } //15.close JdbcUtil.close(connection, preparedStatement, resultSet); //16.返回值 //如果这个集合的长度不等于0,就证明这个集合里面有数据,返回整个集合。 //如果这个集合长度为0,就证明这个集合里面没有数据,返回null return ts.size()!= 0 ? ts: null;//三目运算符 } }
Dao层
package com.javacoffee.dao; import com.javacoffee.entity.User; import com.javacoffee.util.BaseDao; import java.sql.SQLException; import java.util.List; public class UserDao extends BaseDao { public void regint(Object[] parameters) throws SQLException { String sql = "insert into user(username,password) values (?,?)"; super.update(sql,parameters); } public void delete(Object[] parameters) throws SQLException { String sql = "delete from user where username = ?"; super.update(sql,parameters); } public void update(Object[] parameters) throws SQLException { String sql = "update user set username = ?,password = ? where username = ?"; super.update(sql,parameters); } public List<User> login(Object[] parameters) throws SQLException, ClassNotFoundException { String sql = "select * from user where username = ? and password = ?"; return super.query(sql, parameters,User.class); } }
entity层
package com.javacoffee.entity; public class User { private int id; private String username; private String password; public User(){} public User(int id,String name, String password) { this.id=id; this.username = name; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "name='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
service层
package com.javacoffee.servlet; import com.javacoffee.dao.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 java.io.IOException; import java.sql.SQLException; @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); Object[] objects = {username}; UserDao userDao = new UserDao(); try { userDao.delete(objects); request.getRequestDispatcher("DeleteSuccess.jsp").forward(request,response); } catch (SQLException e) { e.printStackTrace(); } } }
package com.javacoffee.servlet; import com.javacoffee.dao.UserDao; import com.javacoffee.entity.User; 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; import java.util.List; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String pwd = request.getParameter("pwd"); Object[] objects = {username,pwd}; UserDao userDao = new UserDao(); try { List<User> login = userDao.login(objects); if (login != null){ request.getRequestDispatcher("/LoginSuccess.jsp").forward(request,response); }else { response.getWriter().append("登陆失败!"); } } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } } }
package com.javacoffee.servlet; import com.javacoffee.dao.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 java.io.IOException; import java.sql.SQLException; @WebServlet("/RegServlet") public class RegServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); Object[] objects = {username,password}; UserDao userDao = new UserDao(); try { userDao.regint(objects); } catch (SQLException e) { e.printStackTrace(); } request.setAttribute("username",username); request.getRequestDispatcher("/success.jsp").forward(request,response); } }
package com.javacoffee.servlet; import com.javacoffee.dao.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 java.io.IOException; import java.sql.SQLException; @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String lastUsername = request.getParameter("lastUsername"); String newUsername = request.getParameter("newUsername"); String newPassword = request.getParameter("newPassword"); Object[] objects = {newUsername,newPassword,lastUsername}; UserDao userDao = new UserDao(); try { userDao.update(objects); request.getRequestDispatcher("UpdateSuccess.jsp").forward(request,response); } catch (SQLException e) { e.printStackTrace(); } } }
效果图:我就是简单的做了做 有很多bug没修复 界面也很丑哈哈哈