package com.sdnu.javaweb.servlet;import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;import java.io.IOException;/*** hello servlet** @author Beyong* @date 2023/03/13 08:48**/
@WebServlet
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {}
}
等同于
等同于
等同于
注解对象的使用格式:
@注解名称(属性名=属性值,属性名=属性值…)
例子:
package com.sdnu.javaweb.servlet;import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;import java.io.IOException;
import java.io.PrintWriter;/*** hello servlet** @author Beyong* @date 2023/03/13 08:48**/
@WebServlet(name="hello", urlPatterns = {"/hello1", "/hello2", "/hello3"}, loadOnStartup = 1,
initParams = {@WebInitParam(name = "username", value = "root"), @WebInitParam(name = "password", value = "123")})
public class HelloServlet extends HttpServlet {public HelloServlet(){System.out.println("无参构造方法执行");}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}
}
package com.sdnu.javaweb.servlet;import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;import java.lang.annotation.Annotation;/*** 反射注解** @author Beyong* @date 2023/03/13 09:58**/
public class ReflectAnnotation {public static void main(String[] args) throws ClassNotFoundException {Class> welcomeServletClass = Class.forName("com.sdnu.javaweb.servlet.HelloServlet");
// boolean annotationPresent = welcomeServletClass.isAnnotationPresent(WebServlet.class);
// System.out.println(annotationPresent);if(welcomeServletClass.isAnnotationPresent(WebServlet.class)){WebServlet webServletAnnotation = welcomeServletClass.getAnnotation(WebServlet.class);String[] value = webServletAnnotation.value();for (int i = 0; i < value.length; i++) {System.out.println(value[i]);}}}
}
package com.sdnu.oa.web.action;import com.sdnu.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/*** 使用模板方法 解决类爆炸** @author Beyong* @date 2023/03/13 10:34**/
@WebServlet({"/dept/edit", "/dept/modify", "/dept/save", "/dept/detail", "/dept/list", "/dept/delete"})
public class DeptServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String servletPath = request.getServletPath();if("/dept/edit".equals(servletPath)){doEdit(request, response);} else if("/dept/modify".equals(servletPath)){doModify(request, response);} else if("/dept/save".equals(servletPath)){doSave(request, response);} else if("/dept/detail".equals(servletPath)){doDetail(request, response);} else if("/dept/list".equals(servletPath)){doList(request, response);} else if("/dept/delete".equals(servletPath)){doDel(request, response);}}private void doEdit(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String contextPath = request.getContextPath();out.print(" ");out.print("");out.print(" ");out.print(" ");out.print(" ");out.print(" ");out.print(" ");out.print(" 修改部门
");out.print("
");out.print(" ");out.print(" ");out.print("");}private void doModify(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");String deptno = request.getParameter("deptno");String dname = request.getParameter("dname");String loc = request.getParameter("loc");Connection conn = null;PreparedStatement ps = null;int count = 0;try {conn = DBUtil.getConnection();String sql = "update dept set dname=?, loc=? where deptno=?";ps = conn.prepareStatement(sql);ps.setString(1, dname);ps.setString(2, loc);ps.setString(3, deptno);count = ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(conn, ps, null);}if(count == 1){request.getRequestDispatcher("/dept/list").forward(request, response);} else {request.getRequestDispatcher("/error.html").forward(request, response);}}private void doSave(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");String deptno = request.getParameter("deptno");String dname = request.getParameter("dname");String loc = request.getParameter("loc");Connection conn = null;PreparedStatement ps = null;int count = 0;try{conn = DBUtil.getConnection();String sql = "insert into dept(deptno, dname, loc) values (?, ?, ?)";ps = conn.prepareStatement(sql);ps.setString(1, deptno);ps.setString(2, dname);ps.setString(3, loc);count = ps.executeUpdate();} catch (SQLException e){e.printStackTrace();}finally {DBUtil.close(conn, ps, null);}if(count == 1){request.getRequestDispatcher("/dept/list").forward(request, response);}else{request.getRequestDispatcher("/error.html").forward(request, response);}}private void doList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String contextPath = request.getContextPath();out.print("");out.print("");out.print(" ");out.print(" ");out.print(" 部门列表页面 ");out.print("");out.print(" ");out.print(" ");out.print(" 部门列表
");out.print("
");out.print(" ");out.print(" ");out.print(" 序号 ");out.print(" 部门列表 ");out.print(" 部门名称 ");out.print(" 操作 ");out.print(" ");//连接数据库,查询所有部门Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {//获取连接conn = DBUtil.getConnection();//获取预编译的数据库操作对象String sql = "select deptno as a, dname, loc from dept";ps = conn.prepareStatement(sql);//执行sql语句rs = ps.executeQuery();//处理结果集int i = 0;while (rs.next()){String deptno = rs.getString("a");String dname = rs.getString("dname");String loc = rs.getString("loc");out.print(" ");out.print(" "+(++i)+" ");out.print(" "+deptno+" ");out.print(" "+dname+" ");out.print(" ");out.print(" 删除");out.print(" 修改");out.print(" 详情");out.print(" ");out.print(" ");}} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(conn, ps, rs);}out.print("
");out.print("
");out.print(" 新增部门");out.print(" ");out.print("");}private void doDetail(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.print(" ");out.print("");out.print(" ");out.print(" ");out.print(" 部门详情 ");out.print(" ");out.print(" ");out.print(" 部门详情
");out.print("
");String deptno = request.getParameter("deptno");Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {conn = DBUtil.getConnection();String sql = "select dname, loc from dept where deptno = ?";ps = conn.prepareStatement(sql);ps.setString(1, deptno);rs = ps.executeQuery();if(rs.next()){String dname = rs.getString("dname");String loc = rs.getString("loc");out.print(" 部门编号:"+deptno+"
");out.print(" 部门名称:"+dname+"
");out.print(" 部门位置:北京
");}} catch (SQLException e) {e.printStackTrace();} finally {DBUtil.close(conn, ps, rs);}out.print(" ");out.print(" ");out.print("");}private void doDel(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String deptno = request.getParameter("deptno");Connection conn = null;PreparedStatement ps = null;int count = 0;try{conn = DBUtil.getConnection();conn.setAutoCommit(false);String sql = "delete from dept where deptno = ?";ps = conn.prepareStatement(sql);ps.setString(1, deptno);count = ps.executeUpdate();conn.commit();} catch (SQLException e){if(conn != null){try {conn.rollback();} catch (SQLException ex) {ex.printStackTrace();}}e.printStackTrace();} finally {DBUtil.close(conn, ps, null);}if(count == 1){request.getRequestDispatcher("/dept/list").forward(request, response);}else{request.getRequestDispatcher("/error.html");}}
}
JSP本质上是一个Servlet
Servlet职责:收集数据(逻辑处理,业务处理,连接数据库)
jsp职责:展示数据(数据的展示)
<%System.out.println("hello jsp");
%>
<%-- 注释内容 --%>