7 Servlet 介绍
7.1 Servlet 的开发
@WebServlet(name = "helloServlet", urlPatterns = {"/helloServlet"}) public class HelloServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); String username = req.getParameter("username"); PrintStream out = new PrintStream(resp.getOutputStream()); out.println(username); } }
启动 tomcat 后使用链接 http://localhost/helloServlet?username=admin 即可。
7.2 Servlet 的配置
从 Servlet 3.0 开始,配置 Servlet 有两种方式:@WebServlet 注解或 web.xml 配置。
7.3 Servlet 的生命周期
7.4 load-on-startup Servlet
创建 Servlet 有两个时机:应用启动时或用户请求时。load-on-startup 参数配置 Servlet 实例化优先级,值越小,越优先。
7.5 访问 Servlet 的配置参数
为 Servlet 配置参数也有两种方式:@WebServlet 的 initParams 属性或 web.xml 中 <servlet> 元素的 <init-param> 子元素。
@WebServlet(name = "helloServlet", urlPatterns = {"/helloServlet"}, initParams = { @WebInitParam(name = "password", value = "admin123") } ) public class HelloServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); PrintStream out = new PrintStream(resp.getOutputStream()); ServletConfig config = getServletConfig(); out.println(config.getInitParameter("password")); } }
7.6 使用 Servlet 作为控制器
在标准的 MVC 模式中,JSP 仅作为表现层技术负责收集用户请求参数并展现数据,Servlet 仅作为控制器使用,所有请求都发给 Servlet,Servlet 调用 Model 处理请求,并调用 JSP 呈现处理结果。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户登录</title> </head> <body> <span> <% String message = (String) request.getAttribute("message"); if (message != null) { out.println(message); } %> </span> 请输入用户名和密码:<br> <form id="login" method="post" action="/loginServlet"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登陆"> </form> </body> </html>
login.jsp
@WebServlet(name = "loginServlet", urlPatterns = {"/loginServlet"}) public class LoginServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); String message = ""; DBUtil dbUtil = new DBUtil(); try { ResultSet resultSet = dbUtil.query("select password from t_user where username = ?", username); if (resultSet.next()) { if (resultSet.getString("password").equals(password)) { // 保存username至会话 HttpSession session = req.getSession(); session.setAttribute("username", username); // 转发到欢迎页面 req.getRequestDispatcher("/welcome.jsp").forward(req, resp); } else { message = "密码错误"; } } else { message = "用户名不存在"; } } catch (Exception e) { e.printStackTrace(); } if (!message.equals("")) { req.setAttribute("message", message); req.getRequestDispatcher("/login.jsp").forward(req, resp); } } }
LoginServlet.java
8 JSP 2 的自动用标签
时间: 2024-11-05 18:57:06