在JSP中,request和response是非常重要的两个东西,请务必知道他们的常用方法。
1、String getParameter(String key) 获取客户端传来的参数。
示例: 我们获取id,name,age 多个参数是通过&符号分开
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
%>
id:<%=id%>,name:<%=name%>,age=<%=age%>
访问地址http://localhost:8080/new_javaweb_test_war_exploded/test2.jsp?id=985&name=xn2001&age=16
2、void setAttribute(String key,Object value) 通过键值对的形式保存数据。
3、Object getAttribute(String key) 通过 key 取出 value。
4、RequestDispatcher getRequestDispatcher(String path) 返回⼀个 RequestDispatcher 对象,该对象的 forward ⽅法⽤于请求转发。
我们对前四个方法做一个小案例,首先我们建立几个jsp页面,分别叫hello1,hello2,hello3
我们让hello1获取一个name参数,保存到key为name中,然后转发给hello2.jsp,hello2不做处理直接转发给hello3.jsp,相关代码如下
hello1.jsp
<body>
<h1>我是Hello1</h1>
<%
//获取name值
String name = request.getParameter("name");
request.setAttribute("name",name);
%>
<%
// 请求分发跳转
request.getRequestDispatcher("hello2.jsp").forward(request,response);
%>
</body>
hello2.jsp
<body>
<h1>我是Hello2</h1>
<%
request.getRequestDispatcher("hello3.jsp").forward(request,response);
%>
</body>
hello3.jsp
<body>
<h1>我是Hello3</h1>
<%=request.getAttribute("name")%>
</body>
我们启动项目,到浏览器中访问hello1.jsp,会发现其实访问的是hello3.jsp中的内容,但是地址栏不改变,name值也成功取出来。
5、String[] getParameterValues() 获取客户端传来的多个同名参数。
实例:
我在test2.jsp中加入该方法的使用,获取多个name,保存为数组。
<body>
<%
String id = request.getParameter("id");
String[] name = request.getParameterValues("name");
String age = request.getParameter("age");
%>
id:<%=id%>,name:<%=Arrays.toString(name)%>,age=<%=age%>
</body>
访问地址/test2.jsp?name=乐心湖&name=心湖博客&id=211&age=16
6、void setCharacterEncoding(String charset) 指定每个请求的编码。中文乱码时需要设置,不乱码一般用不到。
200:正常
404:资源找不到
400:请求类型不匹配
500:Java 程序抛出异常
sendRedirect(String path):重定向,页面之间的跳转。
转发 getRequestDispatcher 和 重定向 sendRedirect 的区别:
转发和重定向有什么区别
转发:同⼀个请求在服务器之间传递,地址栏不变,也叫服务器跳转。
重定向:由客户端发送⼀次新的请求来访问跳转后的⽬标资源,地址栏改变,也叫客户端跳转。
如果两个页面之间需要通过 request 来传值,则必须使用转发,不能使用重定向。
在用户登录功能里,如果⽤户名和密码正确,则跳转到首页(转发),并且展示用户名,否则重新回到登陆页面(重定向)。
我们去写一套完整的登录系统,
首先新建一个login.jsp,里面是一个表单,有用户名,密码,登录
<body>
<form action="check.jsp" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
我们点击登录会提交到check.jsp,所以我们需要去写这个jsp,我们在里面加入验证用户名密码的功能,如果正确就转发给welcome.jsp,因为转发才可以传递request。失败就重定向到login.jsp
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("admin")&&password.equals("123456")){
request.setAttribute("name",username);
request.getRequestDispatcher("welcome.jsp").forward(request,response);
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
我们去写一个简单welcome.jsp
<body>
<%
String name = (String) request.getAttribute("name");
%>
你好<%=name%>,欢迎来到心湖演示。
</body>
启动项目,访问login.jsp,进行测试。效果如下图。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有