JavaWeb学习-JSP系列-2-JSP最佳实践

怼烎@ 2022-01-27 07:17 359阅读 0赞

这篇来讨论下JSP的最佳实践,也就是JSP中应该写什么代码,不应该写什么代码。JSP本质是Servlet,那么我们就要考虑什么时候用Servlet什么时候使用jsp。有些代码确实在servlet中实现,也可以在jsp文件中实现,本篇就是来了解下JSP最佳实践。

1.全部代码写成jsp,模拟用户登录成功。

三个jsp页面,分别是登录框和处理判断登录是否成功页面和登录成功,欢迎用户页面。

20190525205448627.png

这么由于没有servlet类,就不用配置web.xml文件,三个文件代码如下

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>登录</title>
  8. </head>
  9. <body>
  10. <form action="/JSP01/doLogin.jsp" method="post">
  11. 用户名:<input type="text" name="userName" /><br/>
  12. 密码:<input type="password" name="pwd" /><br/>
  13. <input type="submit" value="登录" /><br/>
  14. </form>
  15. </body>
  16. </html>
  17. <%@ page language="java" contentType="text/html; charset=UTF-8"
  18. pageEncoding="UTF-8"%>
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  20. <html>
  21. <head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  23. <title>验证登录</title>
  24. </head>
  25. <body>
  26. <%
  27. //获取表单数据
  28. String userName = request.getParameter("userName");
  29. String pwd = request.getParameter("pwd");
  30. //处理业务逻辑,这里不走mysql DB,模拟一下
  31. if("Anthony".equals(userName) && "123".equals(pwd)){
  32. //跳转到 登录成功,欢迎你页面
  33. request.getRequestDispatcher("/loginSuccess.jsp").forward(request, response);
  34. }else {
  35. //重定向到登录页面
  36. response.sendRedirect("/JSP01/login.jsp");
  37. }
  38. //分发转向
  39. %>
  40. </body>
  41. </html>
  42. <%@ page language="java" contentType="text/html; charset=UTF-8"
  43. pageEncoding="UTF-8"%>
  44. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  45. <html>
  46. <head>
  47. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  48. <title>登录成功</title>
  49. </head>
  50. <body>
  51. 欢迎你:<%
  52. String userName = request.getParameter("userName");
  53. out.print(userName);
  54. %>
  55. </body>
  56. </html>

运行来测试下

20190525205951341.png

20190525210013706.png

这样看起来好像没啥问题对吧,但是我们明明是想要让用户看到的是loginSuccess.jsp,然后显示欢迎用户,这个第二张图显示是doLogin.jsp, 对用户来说,判断用户登录,这个页面应该隐藏,不应该让用户看到,当然用户也不关心和他无关的页面。

2.和用户UI无关代码写到servlet中去

这样我们这里把doLogin中代码提取到一个Servlet类中,其他文件代码。

  1. package com.anthony.login;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class DoLoginServlet extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  10. //获取表单数据
  11. String userName = req.getParameter("userName");
  12. String pwd = req.getParameter("pwd");
  13. //处理业务逻辑,这里不走mysql DB,模拟一下
  14. if("Anthony".equals(userName) && "123".equals(pwd)){
  15. //转发到 登录成功,欢迎你页面
  16. req.getRequestDispatcher("/loginSuccess.jsp").forward(req, resp);
  17. }else {
  18. //重定向到登录页面
  19. resp.sendRedirect("/JSP01/login.jsp");
  20. }
  21. //分发转向
  22. }
  23. @Override
  24. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25. doGet(req, resp);
  26. }
  27. }

在login.jsp中表单中action需要改成这个doLoginServlet的访问url

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>登录</title>
  8. </head>
  9. <body>
  10. <form action="/JSP01/doLoginServlet" method="post">
  11. 用户名:<input type="text" name="userName" /><br/>
  12. 密码:<input type="password" name="pwd" /><br/>
  13. <input type="submit" value="登录" /><br/>
  14. </form>
  15. </body>
  16. </html>

重新部署到tomcat,然后访问,没啥问题。

3.转发和重定向不同,引起拿不到用户名称

这里把doLoginServlet.java中登录成功,把转发改成重定向试试,看看用户名还能不能拿到。

  1. package com.anthony.login;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class DoLoginServlet extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  10. //获取表单数据
  11. String userName = req.getParameter("userName");
  12. String pwd = req.getParameter("pwd");
  13. //处理业务逻辑,这里不走mysql DB,模拟一下
  14. if("Anthony".equals(userName) && "123".equals(pwd)){
  15. //转发到 登录成功,欢迎你页面
  16. //req.getRequestDispatcher("/loginSuccess.jsp").forward(req, resp);
  17. resp.sendRedirect(req.getContextPath() + "/loginSuccess.jsp");
  18. }else {
  19. //重定向到登录页面
  20. resp.sendRedirect(req.getContextPath() + "/login.jsp");
  21. }
  22. //分发转向
  23. }
  24. @Override
  25. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  26. doGet(req, resp);
  27. }
  28. }

登录成功转发改成重定向之后,name的值就拿不到了

20190525214028751.png

4.还是转发,新增一个页面也需要欢迎用户,还是拿不到用户名

这里我们还是改回到转发方法,上面我们知道转发,在登录成功页面,肯定能拿到用户名信息。那么如果跳转一个主页,也需要拿到用户名信息,转发还好使吗。

  1. package com.anthony.login;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class DoLoginServlet extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  10. //获取表单数据
  11. String userName = req.getParameter("userName");
  12. String pwd = req.getParameter("pwd");
  13. //处理业务逻辑,这里不走mysql DB,模拟一下
  14. if("Anthony".equals(userName) && "123".equals(pwd)){
  15. //转发到 登录成功,欢迎你页面
  16. req.getRequestDispatcher("/loginSuccess.jsp").forward(req, resp);
  17. //resp.sendRedirect(req.getContextPath() + "/loginSuccess.jsp");
  18. }else {
  19. //重定向到登录页面
  20. resp.sendRedirect(req.getContextPath() + "/login.jsp");
  21. }
  22. //分发转向
  23. }
  24. @Override
  25. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  26. doGet(req, resp);
  27. }
  28. }
  29. <%@ page language="java" contentType="text/html; charset=UTF-8"
  30. pageEncoding="UTF-8"%>
  31. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  32. <html>
  33. <head>
  34. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  35. <title>Insert title here</title>
  36. </head>
  37. <body>
  38. <h1>欢迎来到首页</h1>
  39. 欢迎你:<%
  40. String userName = request.getParameter("userName");
  41. out.print(userName);
  42. %>
  43. </body>
  44. </html>

测试这个修改

20190525215026794.png

点击这个link

20190525215048733.png

这里来想一下什么拿不到,我们点击跳转主页,这个动作就是触发了一个新的请求,我们知道请求是一个域对象,不同请求对象中,是不能共享数据,那么我们学习过session,下面就用session来解决这个问题。

  1. package com.anthony.login;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class DoLoginServlet extends HttpServlet {
  8. @Override
  9. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  10. //获取表单数据
  11. String userName = req.getParameter("userName");
  12. String pwd = req.getParameter("pwd");
  13. //处理业务逻辑,这里不走mysql DB,模拟一下
  14. if("Anthony".equals(userName) && "123".equals(pwd)){
  15. //转发到 登录成功,欢迎你页面
  16. //转发之前,用户名写入session属性
  17. req.getSession().setAttribute("userName", userName);
  18. req.getRequestDispatcher("/loginSuccess.jsp").forward(req, resp);
  19. //resp.sendRedirect(req.getContextPath() + "/loginSuccess.jsp");
  20. }else {
  21. //重定向到登录页面
  22. resp.sendRedirect(req.getContextPath() + "/login.jsp");
  23. }
  24. //分发转向
  25. }
  26. @Override
  27. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  28. doGet(req, resp);
  29. }
  30. }
  31. <%@ page language="java" contentType="text/html; charset=UTF-8"
  32. pageEncoding="UTF-8"%>
  33. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  34. <html>
  35. <head>
  36. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  37. <title>Insert title here</title>
  38. </head>
  39. <body>
  40. <h1>欢迎来到首页</h1>
  41. 欢迎你:<%
  42. String userName = (String)session.getAttribute("userName");
  43. out.print(userName);
  44. %>
  45. </body>
  46. </html>

得到效果是这样

20190525215439566.png

5.JSP最佳实践总结

  1. Servlet:控制器,重点编写Java代码逻辑(获取表单数据,处理业务逻辑,分发转向)
  2. JSP:代码显示模板,重点在于显示数据。

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读