• 2022.11.09第6次Javaweb上机——实现登录欢迎页面


    前情回顾

    Myeclipse的安装教程_科大云炬的博客-CSDN博客

    【JavaWeb第1次上机练习】安装Tomcat并在本地浏览器成功运行第一个Hello world网站_科大云炬的博客-CSDN博客

    【第2次JavaWeb上机练习】_科大云炬的博客-CSDN博客 

    第3次 JavaWeb上机练习:Servlet基础作业_科大云炬的博客-CSDN博客

    第4次JavaWeb上机练习——ServletConfig接口的常用方法_科大云炬的博客-CSDN博客

    Javaweb第5次上机练习(获取HTTP的请求参数)_科大云炬的博客-CSDN博客

    上机实操

    1在src目录新建名为ServletCkde的servlet

    功能:从前端form表单接收post请求中的参数,并传递给后端的jsp脚本提示欢迎信息

    1. package com.zr;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import javax.servlet.ServletException;
    5. import javax.servlet.http.HttpServlet;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. public class ServletCkde extends HttpServlet {
    9. public ServletCkde() {
    10. super();
    11. }
    12. public void doPost(HttpServletRequest request, HttpServletResponse response)
    13. throws ServletException, IOException {
    14. //指定浏览器汉字的编码格式
    15. response.setContentType("text/html;charset=utf-8");
    16. //指定服务器的汉字编码格式
    17. request.setCharacterEncoding("utf-8");
    18. //1.获取表单中提交的名字
    19. String usernameString =request.getParameter("username");
    20. String passwordString =request.getParameter("password");
    21. //2.跳转到注册成功页面,并且把请求响应参数传递过去
    22. //3.通过request域把获取到的表单数据写入request域中,并通过getRequestDispatcher,
    23. //指定传递页面,通过forward传递参数出去
    24. request.setAttribute("name",usernameString);
    25. request.setAttribute("pwd",passwordString);
    26. request.getRequestDispatcher("/loginok.jsp").forward(request,response);
    27. }
    28. public void destroy() {
    29. super.destroy(); // Just puts "destroy" string in log
    30. // Put your code here
    31. }
    32. public void doGet(HttpServletRequest request, HttpServletResponse response)
    33. throws ServletException, IOException {
    34. }
    35. /**
    36. * Initialization of the servlet.
    37. *
    38. * @throws ServletException if an error occurs
    39. */
    40. public void init() throws ServletException {
    41. // Put your code here
    42. }
    43. }

    2在WebRoot目录新建login.html

    功能:前端交互页面,接收用户输入的用户名、密码信息

    注:在from的action中填写ServletCkde的请求地址(包含项目名+路由地址)

    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    2. <html>
    3. <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
    4. <body>
    5. <form action="/1109/servlet/ServletCkde" method="post">
    6. 姓名:<input type="text"name="username"><br>
    7. 密码:<input type="text"name="password"><br>
    8. <input type="submit"name="提交">
    9. </form>
    10. </body>
    11. </html>

    运行一下页面效果

     

     3在WebRoot目录新建loginok.jsp

    功能:从servlet的接收用户名和密码并提示欢迎信息,并在五秒后跳转到index.jsp首页

     

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <body>
    9. <%
    10. String strname=(String)request.getAttribute("name");
    11. String strpwd=(String)request.getAttribute("pwd");
    12. %>
    13. <p style="color:red">
    14. <%
    15. response.setHeader("refresh","2;ur1=/Webbk/index.jsp");
    16. %>
    17. 欢迎你:<%=strname%>你已成功登录,请牢记您的密码<%=strpwd%>三秒后跳转到首页</p>
    18. 秒后跳转到首页,如没有跳转你可以点击<ahref="index.jsp">首页〈/a>跳转到首页
    19. </body>
    20. </html>

     

    4在WebRoot目录新建index.jsp

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'index.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. <!--
    17. <link rel="stylesheet" type="text/css" href="styles.css">
    18. -->
    19. </head>
    20. <body>
    21. 这是首页 <br>
    22. </body>
    23. </html>

    运行结果

     

     

    项目源码

    https://t.zsxq.com/08FzQOAah

     

  • 相关阅读:
    Cortex-A7 架构
    用hadoop-eclipse-plugins-2.6.0来配置hadoop-3.3.6
    数据结构:二叉树
    计算机网络体系结构——物理层
    Vue——模板引用(不建议使用,了解)
    [Python] 面向对象(二)
    openresty+etcd配置实现原理
    数据结构——排序算法——桶排序
    题目:2715.执行可取消的延迟函数
    MSDC 4.3 接口规范(19)
  • 原文地址:https://blog.csdn.net/qq_39154376/article/details/127939138