JSP 中request与response的用法详解(jsp中response实现页面跳转)这样也行?

随心笔谈2年前发布 编辑
157 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买



JSP 中request与response的用法详解

概要:

在学习这两个对象之前,我们应该已经有了http协议的基本了解了,如果不清楚http协议的可以看我的关于http协议的介绍。因为其实request和response的使用大部分都是对http协议的操作。

request对象的介绍

我们先从request对象进行介绍:

我们知道http协议定义了请求服务器的格式:

请求行

请求头

空格

请求体(get请求没有请求体)

好了,这里我们就不详细介绍了,我们只看几个应用就可以了,没什么难度:

应用1 获取请求头

public class CServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String ip=request.getRemoteAddr();//获取IP地址
String method=request.getMethod();//获取请求方式
System.out.println(ip);
System.out.println(method);
getLiuLanQi(request);
protectMethod(request, response);
}
//防盗链
public void protectMethod(HttpServletRequest request,
HttpServletResponse response) {
String url=request.getHeader(“referer”);
System.out.println(url);
if(url !=null && url.equalsIgnoreCase(“http://localhost:8080/day10/MyHtml.html”) ) {
System.out.println(“hi”);
}
else {
response.setStatus(302);
response.setHeader(“Location”,”/day10/MyHtml.html”);
System.out.println(“重定向”);
}
}
//判断浏览器的类型
public void getLiuLanQi(HttpServletRequest request) {
String user=request.getHeader(“User-Agent”);
if(user !=null) {
String userNoC=user.toLowerCase();
if(userNoC.contains(“chrome”)) {
System.out.println(“浏览器类型:谷歌” );
}
else if(userNoC.contains(“firefox/”)) {
System.out.println(“浏览器类型:火狐”);
}
else {
System.out.println(“浏览器类型:IE”);
}
}
else {
System.out.println(“null”);
}
}
}

应用2获取URL路径

public class DServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getScheme());
System.out.println(request.getServerName());
System.out.println(request.getServerPort());
System.out.println(request.getContextPath());
System.out.println(request.getServletPath());
System.out.println(request.getQueryString());
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
}

}

应用3获取请求参数

public class EServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);//调用他来解决乱码问题的,不会的可以看我写的解决乱码的那一篇
String name=request.getParameter(“name”);
String password=request.getParameter(“password”);
String[] aihao=request.getParameterValues(“aihao”);
response.setHeader(“content-type”, “text/html;charset=utf-8”);
PrintWriter pw=response.getWriter();
pw.println(“name:” + name + “<br/>”);
pw.println(“password:” + password + “<br/>”);
pw.print(“aihao”);
for (int i=0; i < aihao.length; i++) {
switch (i) {
case 1:
pw.print(“吃饭”);
break;
case 2:
pw.print(“睡觉”);
break;
case 3:
pw.print(“打豆豆”);
break;

default:

break;
}
}
pw.close();
}

}

<!– 用于发送请求的html –>
<html>
<head>
<title>denglu.html</title>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>
</head>

<body>
<form action=”/day10/EServlet” method=”post”><!– 通过post方法提交 –>
姓名:<input type=”text” name=”name”/><br/>
密码:<input type=”password” name=”password”><br/>
爱好:<input type=”checkbox” name=”aihao” value=”1″/>吃饭
<input type=”checkbox” name=”aihao” value=”2″/>睡觉
<input type=”checkbox” name=”aihao” value=”3″/>打豆豆
<input type=”submit” value=”提交”>
</form>
</body>
</html>

应用4设置request对象属性

用来实现同一个应用程序servlet之间值得传递,我们还用到了转发

public class FServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute(“name”,”weijinhao”);
request.setAttribute(“password”, “123”);
RequestDispatcher rd=request.getRequestDispatcher(“/GServlet”);
rd.forward(request, response);
}

}

public class GServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=(String)request.getAttribute(“name”);
String password=(String)request.getAttribute(“password”);
response.setHeader(“content-type”, “text/html;charset=utf-8”);
PrintWriter pw=response.getWriter();
pw.println(“name:” + name + “<br/>”);
pw.println(“password:” + password + “<br/>”);
pw.close();
}

}

好了,request的用法大致就是这摸多了。

让我们来看看response对象吧。

response对象的使用

http协议定义了响应浏览器的格式

响应行

响应头

空格

响应体

response对象的大致应用是用在设置响应码,设置响应头,及设置响应正文。

应用1设置响应正文

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//String n=”hello javaweb”;
//byte[] b=n.getBytes();
//字节流的使用

//字符流的使用

//commoms工具包的使用
FileInputStream fileInputStream=new FileInputStream(“C:/Users/74087/Desktop/6226354_994604.jpg”);
byte[] b=IOUtils.toByteArray(fileInputStream);
ServletOutputStream outputStream=response.getOutputStream();
outputStream.write(b);
outputStream.close();
}

}

应用2 设置响应码和响应头

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//sendNum(response);
//sendRe(response);
//refresh(response);
//noCache(response);
}
//禁用浏览器缓存
public void noCache(HttpServletResponse response) {
response.setHeader(“Cache-Control”,”no-cache”);
response.setHeader(“pragma”,”0″);
response.setHeader(“expires”,”no-cache”);
}
//自动跳转
public void refresh(HttpServletResponse response) {
response.setHeader(“refresh”, “5;url=http://www.baidu.com”);
}
//重定向的使用
public void sendRe(HttpServletResponse response) {
response.setStatus(302);
response.setHeader(“Location”, “http://www.baidu.com”);
}

public void sendNum(HttpServletResponse response) throws IOException {
response.setStatus(200);
response.sendError(404);
response.sendError( 404, “ser accept your request but i don’t want to you see my web page”);
}

}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:jsp 使用request为页面添加静态数据的实例JSP中Servlet的Request与Response的用法与区别JSP的request对象实例详解JSP request.setAttribute()详解及实例jsp Request获取url信息的各种方法对比JSP利用过滤器解决request中文乱码问题JSP用过滤器解决request getParameter中文乱码问题Jsp中request的3个基础实践

© 版权声明

相关文章