JavaWeb(Servlet编程)第三章

news/2024/10/18 17:14:52/

一,常用页面跳转方法1:重定向


 通过HttpServletResponse的sendRedirect()方法来跳转到本地URL
 void sendRedirect(String locationURL)
 locationURL形式:URL[?参数名称1=参数值1&参数名称2=参数值2&…]
 只能传递字符串,不能传递对象

<%--Created by IntelliJ IDEA.User: ***Date: 2024/10/13Time: 19:01To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form action="t2" method="post">name:<input type="text" name="name"><br/><input type="submit" value="submit">
</form></body>
</html>

在test02.jsp文件中写出一个表单将name变量传递给标签名为t2的servlet,这里方法为post方法

package 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("/t2")
public class test2 extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String name = req.getParameter("name");resp.sendRedirect("test2.jsp?name="+name);}
}

在标签为t2的servlet中将name变量接收一下,并使用重定向跳转到test2.jsp文件中去,并将name变量的值传递给这个页面,这里 使用的传递参数的方法是问号传参,这样的传递参数的方式,会将重定向括号中的字符串部分和变量值部分全部显示到页面的网址栏中。

<%--Created by IntelliJ IDEA.User: ***Date: 2024/7/20Time: 15:09To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1><%=request.getParameter("name")%>
</h1>
</body>
</html>

并将这个变量输出出来以证明传参成功。

运行结果如下图:

二,解决页面乱码问题

通过HttpServletResponse的setContentType()方法设置字符编码
 setContentType("text/html;charset=UTF-8");
 必须写在响应信息之前
 

三,JSP+Servlet制作简易登陆系统

<%--Created by IntelliJ IDEA.User: ***Date: 2024/10/13Time: 19:43To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form action="cs" method="post">username:<input type="text" name="username"><br/>password:<input type="password" name="password"><br/><input type="submit" value="login"><a href="register2.jsp">if you don't have user,Please click here</a>
</form></body>
</html>

首先创建一个JSP文件login2.jsp使用form表单将用户名和密码传递给后端csServlet代码中,并定义一个超链接可以跳转到注册界面,若没有用户名的话。

<%--Created by IntelliJ IDEA.User: ***Date: 2024/10/13Time: 19:49To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form action="rs" method="post">username:<input type="text" name="username"><br/>password:<input type="password" name="password"><br/><input type="submit" value="register">
</form></body>
</html>

再创建一个注册的页面register2.jsp,将注册的数据传递给标签名为rs的Servlet类中。

package csdn;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.util.HashMap;
import java.util.Map;@WebServlet("/rs")
public class RegisterServlet extends HttpServlet {static Map<String,String> map=new HashMap<>();@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username = req.getParameter("username");String password = req.getParameter("password");map.put(username,password);resp.sendRedirect("login2.jsp");}
}

这是一个注册的后端代码,设置标签为rs,创建类声明一个静态的map集合,用来存储注册过的用户名和密码,接收注册页面的用户名和密码将接收过来的变量存住在map集合中,注册之后就是要回到登录页面继续登录,所以我们使用一个重定向跳转到login2.jsp这个页面。

package csdn;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("/cs")
public class CheckServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username = req.getParameter("username");String password = req.getParameter("password");if (RegisterServlet.map.get(username) != null & RegisterServlet.map.get(username).equals(password)) {resp.sendRedirect("welcome.jsp");} else {resp.sendRedirect("login2.jsp");}}
}

来到cs这个Servlet类,在这个类中是用来判断登录的用户名和密码是否与注册的用户名和密码相同,相同的话就会跳转到welcome.jsp中,如果不同就会跳转到登录页面继续登录,这里选择了map集合的get方法不为空的写法,因为如果查找到map中value的值,就说明传递过来的用户名在集合中存在,就不会输出null,如果不存在就会输出null,之前设置静态结合就是为了选择类名加点的引用.

<%--Created by IntelliJ IDEA.User: ***Date: 2024/10/13Time: 20:02To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><h1>Login Successfully</h1></body>
</html>

这是登录成功的页面代表我们成功登录。接下来请看运行效果。

这里我就注册了一对用户名和密码,要是想多注册也是可以的,但是我们刚刚学习所以这个系统非常不完善,所以当tomcat停止运行时,集合中所记录的键值对就会全部消失,所以只是一次性的,等以后我们连接数据库就可以保存了。


http://www.ppmy.cn/news/1538913.html

相关文章

HTB:Base[WriteUP]

目录 连接至HTB服务器并启动靶机 1.Which two TCP ports are open on the remote host? 2.What is the relative path on the webserver for the login page? 3.How many files are present in the /login directory? 4.What is the file extension of a swap file? …

charAt,chartCodeAt,codePointAt,fromCodePoint,fromCharCode

生僻字的length算2,有些空格是特殊空格,比如\u3000 u3000不是全角空格&#xff0c;u3000是表意字空格&#xff08;Ideographic Space&#xff09;&#xff0c;宽度和一个表意字&#xff08;汉字&#xff09;相同。它应当被当做汉字来处理。比如&#xff0c;在一些排版中&#x…

SpringBoot高校学科竞赛平台:性能优化与实践

3系统分析 3.1可行性分析 通过对本高校学科竞赛平台实行的目的初步调查和分析&#xff0c;提出可行性方案并对其一一进行论证。我们在这里主要从技术可行性、经济可行性、操作可行性等方面进行分析。 3.1.1技术可行性 本高校学科竞赛平台采用SSM框架&#xff0c;JAVA作为开发语…

大模型百科:超详细解读与学习路线图

大模型的定义 大模型是指具有数千万甚至数亿参数的深度学习模型。近年来&#xff0c;随着计算机技术和大数据的快速发展&#xff0c;深度学习在各个领域取得了显著的成果&#xff0c;如自然语言处理&#xff0c;图片生成&#xff0c;工业数字化等。为了提高模型的性能&#xf…

RandLA-Net 基于 Tensorflow , 训练自定义数据集

搭建 RandLA-Net 训练环境, 生成自定义训练数据集, 训练自定义数据集. Code: https://github.com/QingyongHu/RandLA-Net 搭建训练环境 Clone the repositorygit clone --depth=1 https://github.com/QingyongHu

Mac 安装 Telnet 工具

方案一 通过 brew install telnet 时会要求安装 xcode 安装命令 brew install telnet报错信息&#xff1a; Warning: No remote origin in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-services, skipping update! Running brew update --auto-update... > Auto-…

SpringMVC Controller返回值技巧:ModelAndView vs String的实战对比

前言 SpringMVC的相关小细节较多&#xff0c;这个博客主要针对控制层&#xff08;Controller&#xff09;中控制器方法的返回值为ModelAndView类型和返回值为String类型区别做出比较和案例实现 第一步&#xff1a;创建web项目&#xff0c;添加依赖&#xff0c;配置web.xml 添加…

ubuntu 开放 8080 端口快捷命令

文章目录 查看防火墙状态开放 80 端口开放 8080 端口开放 22端口开启防火墙重启防火墙**使用 xhell登录**&#xff1a; 查看防火墙状态 sudo ufw status [sudo] password for crf: Status: inactivesudo ufw enable Firewall is active and enabled on system startup sudo…