JSP 中request与response的用法详解(2)

<!-- 用于发送请求的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"/><br/> 密码:<input type="password"><br/> 爱好:<input type="checkbox" value="1"/>吃饭 <input type="checkbox" value="2"/>睡觉 <input type="checkbox" value="3"/>打豆豆 <input type="submit" value="提交"> </form> </body> </html>

应用4设置request对象属性

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

/** * ** request域     Servlet中三大域对象:request、session、application,都有如下三个方法:     > void setAttribute(String name, Object value)     > Object getAttribute(String name) > void removeAttribute(String name);     > 同一请求范围内使用request.setAttribute()、request.getAttribute()来传值!前一个Servlet调用setAttribute()保存值,后一个Servlet调用getAttribute()获取值。 * */ 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设置响应正文

/** * response的正文 * 1,字符流 * 2,字节流 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String n = "hello javaweb"; //byte[] b = n.getBytes(); //字节流的使用 /*ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(b); outputStream.close();*/ //字符流的使用 /*PrintWriter p = response.getWriter(); p.write(n); p.close();*/ //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 设置响应码和响应头

/** *response 的响应码 *1, sendError(int sc)//发送错误代码 *2, sendError(int sc, java.lang.String msg) //发送错误代码及描述 *3, setStatus(int sc) //发送成功响应代码 *response 的响应头 *1, setDateHeader(java.lang.String name, long date) *2, setHeader(java.lang.String name, java.lang.String value) *3, setIntHeader(java.lang.String name, int value) * 下面的和上面的作用一样但是是为了进行多值的添加 * addDateHeader(java.lang.String name, long date) * addHeader(java.lang.String name, java.lang.String value) * addIntHeader(java.lang.String name, int value) */ 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"); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjzsg.html