JSP内置对象:Request和Response的简单介绍及使用

所属分类: 网络编程 / JSP编程 阅读数: 960
收藏 0 赞 0 分享

JSP内置对象之request对象
客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。
序号 方 法 说 明
1 object getAttribute(String name) 返回指定属性的属性值
2 Enumeration getAttributeNames() 返回所有可用属性名的枚举
3 String getCharacterEncoding() 返回字符编码方式
4 int getContentLength() 返回请求体的长度(以字节数)
5 String getContentType() 得到请求体的MIME类型
6 ServletInputStream getInputStream() 得到请求体中一行的二进制流
7 String getParameter(String name) 返回name指定参数的参数值
8 Enumeration getParameterNames() 返回可用参数名的枚举
9 String[] getParameterValues(String name) 返回包含参数name的所有值的数组
10 String getProtocol() 返回请求用的协议类型及版本号
11 String getScheme() 返回请求用的计划名,如:http.https及ftp等
12 String getServerName() 返回接受请求的服务器主机名
13 int getServerPort() 返回服务器接受此请求所用的端口号
14 BufferedReader getReader() 返回解码过了的请求体
15 String getRemoteAddr() 返回发送此请求的客户端IP地址
16 String getRemoteHost() 返回发送此请求的客户端主机名
17 void setAttribute(String key,Object obj) 设置属性的属性值
18 String getRealPath(String path) 返回一虚拟路径的真实路径
复制代码 代码如下:

< %@ page contentType="text/html;charset=gb2312"%>
< %request.setCharacterEncoding("gb2312");%>
< html>
< head>
< title>request对象_例1< /title>
< /head>
< body bgcolor="#FFFFF0">
< form action="" method="post">
< input type="text" name="qwe">
< input type="submit" value="提交">
< /form>
请求方式:< %=request.getMethod()%>< br>
请求的资源:< %=request.getRequestURI()%>< br>
请求用的协议:< %=request.getProtocol()%>< br>
请求的文件名:< %=request.getServletPath()%>< br>
请求的服务器的IP:< %=request.getServerName()%>< br>
请求服务器的端口:< %=request.getServerPort()%>< br>
客户端IP地址:< %=request.getRemoteAddr()%>< br>
客户端主机名:< %=request.getRemoteHost()%>< br>
表单提交来的值:< %=request.getParameter("qwe")%>< br>
< /body>
< /html>  
< %@ page contentType="text/html;charset=gb2312"%>
< %request.setCharacterEncoding("gb2312");%>
< %@ page import="java.util.Enumeration"%>
< html>
< head>
< title>request对象_例2< /title>
< /head>
< body bgcolor="#FFFFF0">
< form action="" method="post">
用户名:< input type="text" name="username">   
密 码:< input type="text" name="userpass">   
< input type="submit" value="进入" >
< /form>
< % 
String str=""; 
if(request.getParameter("username")!=null && request.getParameter("userpass")!=null){ 
Enumeration enumt = request.getParameterNames(); 
while(enumt.hasMoreElements()){ 
str=enumt.nextElement().toString(); 
out.println(str ":" request.getParameter(str) "< br>"); 


%>
< /body>
< /html>  
< %@ page contentType="text/html;charset=gb2312"%>
< %request.setCharacterEncoding("gb2312");%>
< html>
< head>
< title>request对象_例3< /title>
< /head>
< body bgcolor="#FFFFF0">
< form action="" method="post">
擅长:< input type="checkbox" name="cb" value="ON1">VC   
< input type="checkbox" name="cb" value="ON2">JAVA  
< input type="checkbox" name="cb" value="ON3">DELPHI  
< input type="checkbox" name="cb" value="ON4">VB  
< br>
< input type="submit" value="进入" name="qwe">
< /form>
< % 
if(request.getParameter("qwe")!=null ){ 
for(int i=0;i< request.getParameterValues("cb").length;i ){ 
out.println("cb" i ":" request.getParameterValues("cb")[i] "< br>"); 

out.println(request.getParameter("qwe")); 

%>
< /body>
< /html>

JSP内置对象之response对象
response对象包含了响应客户请求的有关信息,但在JSP中很少直接用到它。它是HttpServletResponse类的实例。
序号 方 法 说 明
1 String getCharacterEncoding() 返回响应用的是何种字符编码
2 ServletOutputStream getOutputStream() 返回响应的一个二进制输出流
3 PrintWriter getWriter() 返回可以向客户端输出字符的一个对象
4 void setContentLength(int len) 设置响应头长度
5 void setContentType(String type) 设置响应的MIME类型
6 sendRedirect(java.lang.String location) 重新定向客户端的请求
更多精彩内容其他人还在看

Java Web实现的基本MVC实例分析

这篇文章主要介绍了Java Web实现的基本MVC,以完整实例形式较为详细的分析了JSP实现MVC架构的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp中调用java代码小结

大多数情况下, jsp 文档的大部分由静态文本(html)构成, 为处理该页面而创建的 servlet 只是将它们原封不动的传递给客户端
收藏 0 赞 0 分享

jsp和servlet操作mysql中文乱码问题的解决办法

自己做测试的时候用到jsp/servlet 向mysql中写数据,但是中文总是乱码,今早纠结了半天才搞定,分享给大家我的解决办法
收藏 0 赞 0 分享

jsp跳转getRequestDispatcher()和sendRedirect()的区别

这篇文章主要介绍了jsp跳转getRequestDispatcher()和sendRedirect()的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

java操作mysql入门代码实例(含插入、更新和查询)

这篇文章主要介绍了java操作mysql代码实例,通过执行SQL语句实现,需要的朋友可以参考下
收藏 0 赞 0 分享

struts2中一个表单中提交多个请求的例子(多个提交按钮)

在很多Web应用中,为了完成不同的工作,一个HTML form标签中可能有两个或多个submit按钮,Struts2中提供了另外一种方法,使得无需要配置可以在同一个action类中执行不同的方法(默认执行的是execute方法)
收藏 0 赞 0 分享

jsp中使用javabean实例介绍

这篇文章主要介绍了jsp中使用javabean的实例,同时介绍了相关属性,需要的朋友可以参考下
收藏 0 赞 0 分享

servlet中session简介和使用例子

在servlet中,session是封装在javax.servlet.http.HttpSession这个接口中的,这个接口是构建在cookie或者URL重写的基础上,要得到一个HttpSession的实例,就可以通过HttpServletRequest的getSession()
收藏 0 赞 0 分享

JSP中用回车监听按钮事件兼容火狐 IE等主流浏览器

这篇文章主要介绍了用回车监听按钮事件并且兼容火狐、IE等主流浏览器 ,需要的朋友可以参考下
收藏 0 赞 0 分享

JAVA velocity模板引擎使用实例

这篇文章主要介绍了JAVA velocity模板引擎使用实例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多