request 对象和 response 对象
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象
HttpServletResponse
HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。
1.向客户端输出中文数据(字节)
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test5(response);
} //输出整数:最好转换成字符串输出
private void test5(HttpServletResponse response) throws IOException{
int i = 98;
response.getOutputStream().write((i+"").getBytes());
} //以字节流用默认编码向客户端输出中文数据:没有乱码
//默认的是GBK
private void test1(HttpServletResponse response) throws IOException{
String s = "你好!一";
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes());
} //以字节流用utf-8编码向客户端输出中文数据
//以UTF-8编码发送数据,浏览器(默认用GB2312)会出现乱码
private void test2(HttpServletResponse response) throws IOException{
String s = "你好!二";
//通知客户端查看UTF-8码表
response.setHeader("Content-Type", "text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes("UTF-8"));
} //以字节流用utf-8编码向客户端输出中文数据
private void test3(HttpServletResponse response) throws IOException{
String s = "你好!三";
ServletOutputStream out = response.getOutputStream();
out.write("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>".getBytes("UTF-8"));
out.write(s.getBytes("UTF-8"));
} //以字节流用utf-8编码向客户端输出中文数据
private void test4(HttpServletResponse response) throws IOException{
String s = "你好!四";
response.setContentType("text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
out.write(s.getBytes("UTF-8"));
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
分析:出现乱码的图解
解决办法:
1)通过更改浏览器的编码方式:IE/”查看”/”编码”/”UTF-8”(不可取)
2)通过设置响应头告知客户端编码方式:response.setHeader(“Content-type”, “text/html;charset=UTF-8”);//告知浏览器数据类型及编码
3)通过meta标签模拟请求头:out.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />".getBytes());
4)通过以下方法:response.setContentType("text/html;charset=UTF-8");
2.向客户端输出中文数据(字符)
package cn.lsl.response; import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test3(response);
} //字符流输出中文数据
private void test1(HttpServletResponse response) throws IOException{
String s = "你好!一";
response.setCharacterEncoding("UTF-8"); //设置查的UTF-8(对内容进行编码,查utf-8)
response.setHeader("Content-Type", "text/html;charset=UTF-8");//通知浏览器以utf-8打开
PrintWriter out = response.getWriter();
out.write(s);//默认查的是ISO-8859-1码表
}
//字符流输出中文数据
private void test2(HttpServletResponse response) throws IOException{
String s = "你好!二";
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
}
//字符流输出中文数据
private void test3(HttpServletResponse response) throws IOException{
String s = "你好!三";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write(s);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
分析:
乱码原因:以默认编码发送数据 ISO-8859-1(没有中国二字编码),此时会发生乱码
解决办法:
1)
setCharacterEncoding(“UTF-8”);//更改编码为UTF-8
response.setHead(“Context-type”,”text/html;charset=UTF-8”);//告诉客户端编码方式
2)response.setContentType("text/html;charset=utf-8");
在字符流输出中文数据时:
response.setContentType("text/html;charset=utf-8");有两个作用:
通知字符流以UTF-8编码输出
通知客户端以UTF-8解码显示
3.输出随机验证码
package cn.lsl.response; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通知浏览器不要缓存
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache"); int width = 120;
int height = 25;
//创建一副内存图像BufferedImage
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//得到属于该图片的画笔:Graphics();
Graphics g = image.getGraphics();
//画边框
g.setColor(Color.BLUE);
g.drawRect(0, 0, width, height);
//填充背景色
g.setColor(Color.YELLOW);
g.fillRect(1, 1, width-2, height-2);
//画干扰线
g.setColor(Color.GRAY);
Random r = new Random();
for(int i=0; i<10; i++)
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
//随机数字
g.setColor(Color.RED);
g.setFont(new Font("宋体",Font.BOLD|Font.ITALIC,20));
int x = 23;
for(int i=0; i<4; i++){
g.drawString(r.nextInt(10)+"", x, 20);
x = x+20;
}
//输出到浏览器的页面上:ImageIO
ImageIO.write(image, "jpg", response.getOutputStream());
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
4.定时刷新
package cn.lsl.response; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo4 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test2(response);
} //定时刷新到自己
private void test1(HttpServletResponse response) throws IOException{
Random r = new Random();
int i = r.nextInt();
response.setIntHeader("Refresh", 1);
response.getOutputStream().write((i+"").getBytes());
} //刷到别的页面
private void test2(HttpServletResponse response) throws IOException{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
response.setHeader("Refresh", "2;URL=/Response/index.jsp");
out.write("登陆成功!2秒后将跳转到主页!若没有跳转,请猛点<a href='/Response/index.jsp'>这里</a>");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
5.控制缓存时间
package cn.lsl.response; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ResponseDemo5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //缓存一小时 response.setDateHeader("Expires", System.currentTimeMillis()+60*60*1000); response.getWriter().write("hello"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
6.请求重定向
请求重定向:一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之 为请求重定向。
地址栏会连,并发送2次请求,增加服务器负担。
实现方式:response.sendRedirect();
原理:302/307状态码和location头即可实现重定向
package cn.lsl.response;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo6 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setStatus(302);
// response.setHeader("Location", "/Response/index.jsp");
response.sendRedirect("/Response/index.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
7.getOutputStream和getWriter方法分别用于得到输出二进制数据、输出文本数据的ServletOuputStream、Printwriter对象。
getOutputStream和getWriter这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。会抛异常。
异常为:getOutputStream() has already been called for this response
package cn.lsl.response; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //字节流和字符流同时使用,报错
//getOutputStream() has already been called for this response
public class ResponseDemo7 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s1 = "a";
response.getOutputStream().write(s1.getBytes());
String s2 = "b";
response.getWriter().write(s2);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }
注:通过字符流或字节流输出的数据并不是直接打给浏览器的。而是把数据写到response对象的缓存中的。服务器从缓存中取出数据,按照HTTP协议的响应格式输出给浏览器。如果你调用的response的输出流没有主动关闭,服务器会替你关的。
HttpServletRequest
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象的方法,可以获得客户这些信息。
1.获取客户机信息的方法
getRequestURL方法返回客户端发出请求时的完整URL。
getRequestURI方法返回请求行中的资源名部分。
getQueryString 方法返回请求行中的参数部分。
getRemoteAddr方法返回发出请求的客户机的IP地址
getRemoteHost方法返回发出请求的客户机的完整主机名
getRemotePort方法返回客户机所使用的网络端口号
getLocalAddr方法返回WEB服务器的IP地址。
getLocalName方法返回WEB服务器的主机名
getMethod得到客户机请求方式
eg:
package cn.lsl.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //获取客户机信息
public class RequestDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取URL和URI
//url:http://localhost:8080/Request/servlet/RequestDemo1
//uri:/Request/servlet/RequestDemo1
System.out.println("url:"+request.getRequestURL());
System.out.println("uri:"+request.getRequestURI()); //通过getQueryString可以获得get方式提交查询串url中?后面部分
System.out.println("queryString:"+request.getQueryString());
//获得客户端ip
System.out.println("ip:"+request.getRemoteAddr());
//通过getContextPath获得工程虚拟目录名称
System.out.println("工程虚拟目录名称:"+request.getContextPath());
//通过getMethod获得请求方式
System.out.println("请求方式:"+request.getMethod());
//获得当前访问资源路径 ----/servlet/RequestDemo1
System.out.println("当前访问资源路径:"+request.getRequestURI().substring(request.getContextPath().length()));
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
注:
1)URL和URI的区别
URL: http://localhost:8080/Request/servlet/RequestDemo1
URI: /Request/servlet/RequestDemo1
URI 包含 URL的 ,URL 一定完成路径,URI可以相对路径
http://localhost:8080/Request/servlet/RequestDemo1
是一个URL 同时也是 URI
/Request/servlet/RequestDemo1 ---- 都是URI 不是URL
2)获得当前访问资源路径 :
request.getRequestURI().substring(request.getContextPath().length());
2.获取请求头信息
getHeader 获得头信息的值,转换一个字符串
getHeaders 获得头信息值 ,获得Enumeration
getHeaderNames 获得所有头信息名称 返回 Enumeration
eg:
package cn.lsl.request; import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RequestDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取指定头信息字段
String value = request.getHeader("Accept-Encoding");
System.out.println(value);
System.out.println("---------------");
//获得所有头信息内容
Enumeration<String> enumeration = request.getHeaderNames();
while(enumeration.hasMoreElements()){
String name = enumeration.nextElement();
System.out.println(name+":"+request.getHeader(name));
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }
3.防盗链程序
referer.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>referer.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head> <body>
<h1>通过链接访问RequestDemo3 不是盗链</h1>
<a href="/Request/servlet/RequestDemo3">link</a>
</body>
</html>
package cn.lsl.request; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RequestDemo3 extends HttpServlet { //防盗链
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过getHeader获得referer头信息
String referer = request.getHeader("referer");
//判断头信息可以知道是不是盗链
response.setContentType("text/html;charset=utf-8");
if(referer == null || !referer.startsWith("http://localhost")){
//盗链
response.getWriter().println("是盗链");
}else{
//不是盗链
response.getWriter().println("真不是盗链");
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
4.获取客户机请求参数
常用API四个
getParameter
getParameterValues
getParameterNames
getParameterMap
eg:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>request.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head> <body>
<h1>分别以get和post方式提交数据</h1>
<h1>第一种:通过超链接提交数据以?方式</h1>
<a href="/Request/servlet/RequestDemo4?name=zhangsan&city=shenzhen">超链接提交数据</a>
<h1>第二种:通过form的post方式提交数据</h1>
<form action="/Request/servlet/RequestDemo4" method="post">
姓名<input type="text" name="name" /><br/>
城市<input type="text" name="city" /><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
package cn.lsl.request; import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RequestDemo4 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
System.out.println(name);
String city = request.getParameter("city");
System.out.println(city);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
5.获取复杂表单的数据及解决中文乱码问题
regist.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>regist.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head> <body>
<form action="/Request/servlet/RequestDemo5" method="post">
<table>
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="username" />
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="password" name="password" />
</td>
</tr>
<tr>
<td>
性别
</td>
<td>
<input type="radio" name="gender" value="male" />
男
<input type="radio" name="gender" value="female" />
女
</td>
</tr>
<tr>
<td>
爱好
</td>
<td>
<input type="checkbox" name="hobby" value="sport" />
运动
<input type="checkbox" name="hobby" value="music" />
音乐
<input type="checkbox" name="hobby" value="game" />
游戏
</td>
</tr>
<tr>
<td>
城市
</td>
<td>
<select name="city">
<option value="beijing">
北京
</option>
<option value="shanghai">
上海
</option>
<option value="shenzhen">
深圳
</option>
</select>
</td>
</tr>
<tr>
<td>
个人简介
</td>
<td>
<textarea rows="5" cols="60" name="introduce"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="注册" />
</td>
</tr>
</table>
</form>
</body>
</html>
package cn.lsl.request; import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set; import javax.activation.URLDataSource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RequestDemo5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决post乱码
request.setCharacterEncoding("utf-8");
//通过getParameter获得指定数据
String username = request.getParameter("username");
System.out.println(username); //解决get乱码
// username = URLEncoder.encode(username, "ISO-8859-1"); //用ISO-8859-1编码
// username = URLDecoder.decode(username, "utf-8"); //用utf-8解码
// username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
// System.out.println(username);
String gender = request.getParameter("gender");
System.out.println(gender); //获取checkbox所有提交数据 --- getParameterValues
String[] hobbies = request.getParameterValues("hobby");
System.out.println(Arrays.toString(hobbies)); //打印所有请求提交的参数
//方式一
Enumeration<String> names = request.getParameterNames();
while(names.hasMoreElements()){
String name = names.nextElement(); //获取每一个参数的名称
System.out.println(name + ":" +Arrays.toString(request.getParameterValues(name)));
}
System.out.println("----------------");
Map<String, String[]> parameterMap = request.getParameterMap();
Set<String> keys = parameterMap.keySet();
for (String key : keys) {
System.out.println(key + ":" + Arrays.toString(parameterMap.get(key)));
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }
注:解决中文乱码问题
//解决post乱码
request.setCharacterEncoding("utf-8");
//解决get乱码(手动编码)
username = URLEncoder.encode(username, "ISO-8859-1"); //用ISO-8859-1编码
username = URLDecoder.decode(username, "utf-8"); //用utf-8解码
或
username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
6.请求转发
1)request对象实现请求转发:请求转发指一个web资源收到客户端请求后,通知服务器调用另外一个web资源进行处理。
2)request对象提供了一个getRequestDispatcher方法,该方法返回一个RequestDispatcher对象,调用这个对象的forward方法可以实现请求转发。
3)request对象同时也是一个域对象,开发人员通过request对象在实现转发时,把数据通过request对象带给其它web资源处理。
eg:AServlet转发到BServlet
package cn.lsl.request; import java.io.IOException; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过request对象传递
//向request域对象保存一个属性
request.setAttribute("name", "lsl");
//传递数据给BServlet
RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet/BServlet");
dispatcher.forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }
package cn.lsl.request; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class BServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getAttribute("name"));
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }
注:1、ServletContext对象的getRequestDispather(String path1)
path1必须用绝对路径,即以”/”开头,若用相对路径会抛出异常 IllegalArgumentException
2、ServletRequest对象的getRequestDispatcher(String path2) path2可以用绝对路径也可以用相对路径
拓展:转发与重定向区别
1).转发一次请求、一次响应。重定向两次请求,两次响应
2).转发只能跳转到站内程序,重定向可定向任何站点
3).转发URL地址不变,重定向URL地址改变
4).转发对客户端不可见,重定向对客户度可见
5).转发共享一个Request中数据,重定向两次请求,不同Request对象,不能共享Request数据
7.包含
logo.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>logo.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head> <body>
<h1>LOGO</h1>
</body>
</html>
footer.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>footer.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head> <body>
<h1>@COPY</h1>
</body>
</html>
package cn.lsl.request; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MainServlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/logo.html").include(request, response);
response.getOutputStream().write("<h1>Main 1</h1>".getBytes());
request.getRequestDispatcher("/footer.html").include(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} }
package cn.lsl.request; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MainServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/logo.html").include(request, response);
response.getOutputStream().write("<h1>Main 2</h1>".getBytes());
request.getRequestDispatcher("/footer.html").include(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
request 对象和 response 对象的更多相关文章
- django HTTP请求(Request)和回应(Response)对象
Django使用request和response对象在系统间传递状态.—(阿伦)当一个页面被请示时,Django创建一个包含请求元数据的 HttpRequest 对象. 然后Django调入合适的视图 ...
- request对象和response对象,什么时候用,具体用哪一个,没有感觉
request对象和response对象,什么时候用,具体用哪一个,没有感觉
- JAVA-JSP内置对象之response对象
相关资料:<21天学通Java Web开发> response对象1.response对象用来给客户端传送输出信息. 方法 ...
- request对象和response对象
Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...
- Request对象和Response对象 JsonResponse对象 和 Django shortcut functions 和QueryDict对象
request request属性 属性: django将请求报文中的请求行.头部信息.内容主体封装成 HttpRequest 类中的属性. 除了特殊说明的之外,其他均为只读的. ''' 0.Http ...
- django的request对象和response对象
概述Django 使用 request 和 response 对象表示系统状态数据..当请求一个页面时,Django创建一个 HttpRequest 对象.该对象包含 request 的元数据. 然后 ...
- Request对象与Response对象
1.Request对象 Request对象是来获取请求消息的,是由服务器(Tomcat)创建的. Request对象继承体系结构: ServletRequest -- 接口 ...
- django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统
Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...
- Scrapy 中 Request 对象和 Response 对象的各参数及属性介绍
Request 对象 Request构造器方法的参数列表: Request(url [, callback=None, method='GET', headers=None, body=None,co ...
随机推荐
- 如何创建Vim Dotfile?
Dotfile是电脑系统里的隐藏文件,它是专门给更高级的用户,如开发者.程序员或工程师使用的,让他们用来调整系统.如何创建Vim-Dotfile? 可以参考以下步骤: 1. 首先,你要检查一下.vim ...
- 使用Oracle官方巡检工具ORAchk巡检数据库
ORAchk概述 ORAchk是Oracle官方出品的Oracle产品健康检查工具,可以从MOS(My Oracle Support)网站上下载,免费使用.这个工具可以检查Oracle数据库,Gold ...
- 基于SignalR实现B/S系统对windows服务运行状态的监测
通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...
- C语言动态走迷宫
曾经用C语言做过的动态走迷宫程序,先分享代码如下: 代码如下: //头文件 #include<stdio.h> #include<windows.h>//Sleep(500)函 ...
- VS2015在创建项目时的一些注意事项
一.下面是在创建一个新的项目是我最常用的,现在对他们一一做一个详细的介绍: 1.Win32控制台应用程序我平时编写小的C/C++程序都用它,它应该是用的最多的. 2.名称和解决方案名称的区别:名称是项 ...
- 微软开源代码编辑器monaco-editor
官网上给出:”The Monaco Editor is the code editor that powers VS Code. A good page describing the code edi ...
- Xcode 锁终端
锁终端 输入: <1>cd /Applications/Xcode.app 回车 结果显示: Xcode.app 输入: <2>sudo chown -hR root:whee ...
- TFS2013 设置签出独占锁
转载自: http://www.cnblogs.com/zhang888/p/4280251.html
- 在VMware上安装CentOS -7
1.下载好VMware 2.准备好CentOS的镜像文件 3.打开VMware创建新的虚拟机 选择自定义高级后按下一步 继续下一步 选择稍后安装操作系统 客户机操作系统选择Linux,版本选择Cent ...
- MzBlog分析
早上衣明志 在QQ群里说他的博客开源了,地址在 https://github.com/qihangnet/MZBlog,基于NancyFX和MongoDB开发的.博客内容需要使用 MarkDown 进 ...