㈠ arangodb在java语言下的测试如何request请求
HttpServletRequest request = ServletActionContext.getRequest();
这样就可以了
㈡ java request和response的区别
当我们向后台发送东西的时,后台就需要用request来进行接收,如果需要给前台返回数据响应时,就会用到response
联系生活 你向别人借东西 其实就是一次请求 request 别人接收到这个请求时 会判断自己是否有您需要的 然后给你回答或者把东西给你 就是response
㈢ java中Request对象的主要方法有哪些
getAttribute(String name):返回由name指定的属性值
getAttributeNames():返回request对象所有属性的名字集合,结果是一个枚举的实例
getCookies():返回客户端的所有Cookie对象,结果是一个Cookie数组
getCharacterEncoding():返回请求中的字符编码方式
getContentLength():返回请求的Body的长度实例
getInputStream():返回请求的输入流,用于获得请求中的数据
getMethod():获得客户端向服务器端传送数据的方法
getParameter(String name):获得客户端传送给服务器端的有name指定的参数值
getParameterNames():获得客户端传送给服务器端的所有参数的名字,结果是一个枚举的实例
getParameterValues(String name):获得有name指定的参数的所有值
getProtocol():获取客户端向服务器端传送数据所依据的协议名称
getQueryString():获得查询字符串
getRequestURI():获取发出请求字符串的客户端地址
getRemoteAddr():获取客户端的IP地址
getRemoteHost():获取客户端的名字
getSession([Boolean create]):返回和请求相关Session
getServerName():获取服务器的名字
getServletPath():获取客户端所请求的脚本文件的路径
例外可以在开发工具中写request后写“.” 工具或自动把可以调用的方法提供给你
㈣ java怎么创建Request
java怎么创建Reques步骤如下:
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext servletContext = ServletActionContext.getServletContext();
request.setAttribute("req", "请求范围属性");
request.getSession().setAttribute("ses", "会话范围属性");
servletContext.setAttribute("app", "应用范围属性");
HttpServletResponse response = ServletActionContext.getResponse();
㈤ java中request是个什么东东,干什么用的
request这个对象不用事先宣告,就可以在JSP网页中使用,在转译为Servlet之后,它会转换为javax.servlet.http.HttpServletRequest型态的对象,HttpServletRequest对象是有关于客户端所发出的请求之对象,只要是有关于客户端请求的信息,都可以借由它来取得,例如请求标头、请求方法、请求参数、使用者IP等等信息。
request的主要方法:
getParameterNames():取得客户端所发出的请求参数名称.
getParameter():可以让您指定请求参数名称,以取得对应的设定值.
getServerName():请求的服务器.
getProtocol():使用协议.
getMethod():请求方法.
getServerPort():请求端口号.
getContextPath():Context路径.
getServletPath(): Servlet路径.
getRequestURI():URI路径.
getQueryString():查询字符串.
getRemoteAddr():使用者主机IP.
getRemotePort():使用者使用端口号.
简单来说就是取值用的。
㈥ java如何在一个普通的类中获取request对象
你是指你的 web 项目中使用到一个工具性的类,它的形参中没有 HttpRequest 或 HttpSession 参数?
如果是这样的话,我们需要使用一个 ThreadLocal 变量,我们把当前 request 的变量绑定到里面,在一个 request 请求的生命周期内我们在方法调用的各个更深的层次中都可以直接使用它而不需要在每个方法中都传递这个 request 参数,保存在某个地方就容易导致因为多个请求共用同一个实例而出问题,所有 context 相关的变量不保存在任何业务类相关并且可能被多线程共用的对象实例中才是保证不会出现线程安全问题的途径。
例如,这个例子中我们只要把web.xml中配置好 ContextFilter 后它就会自动在请求开始时绑定一个 context,结束后自动销毁,在这中间的任何时刻我们都可以通过 MyWebContext.getCurrentContext() 得到我们的 HttpServletRequest 实例和其它相关的 context 变量:
//MyWebContext记录当前Request的所有context变量。因为servlet是一个请求绑定一个线程的,我们用ThreadLocal不会有线程安全问题。
classMyWebContext{
=newThreadLocal();
//拿出当前线程绑定的context
(){
return(MyWebContext)contexts.get();
}
(){
returnnewMyWebContext();
}
//绑定一个context到当前线程
publicstaticvoidsetContext(MyWebContextcontext){
contexts.set(context);
}
publicstaticvoidclearContext(){
contexts.set(null);
}
privateHttpRequestrequest;
publicvoidsetRequest(HttpRequestrequest){
this.request=request;
}
publicHttpRequestgetRequest(){
returnthis.request;
}
}
{
publicvoiddoFilter(ServletRequest,SerlvetResponse,FilterChainchain){
//创建并绑定我们的context
MyWebContextcontext=MyWebContext.createContext();
context.setRequest(request);
MyWebContext.setContext(context);
try{
chain.doFilter(request,response);
}finally{
//销毁context
MyWebContext.clearContext();
}
}
}
{
(){
//其它方法只要它是工作在servlet请求调用键中间的某个时刻,它就肯定能拿到Filter绑定进去的Request,这样我们就不必要在每次方法调用中都额外地传递一个HttpRequest参数,当调用层次很深时这能明显减少复杂性。
MyWebContextcontex=MyWebContext.getCurrentContext();
HttpRequestrequest=context.getRequest();
=...;
HttpSessionsession=request.getSession(false);
...
}
}
㈦ java request 如何取到发送请求的地址是什么
request对象通过以下方法来获取请求路径,如下所示:
String getServerName():获取服务器名,localhost;
String getServerPort():获取服务器端口号,8080;
String getContextPath():获取项目名,/Example;
String getServletPath():获取Servlet路径,/AServlet;
String getQueryString():获取参数部分,即问号后面的部分:username=zhangsan
String getRequestURI():获取请求URI,等于项目名+Servlet路径:/Example/AServlet
String getRequestURL():获取请求URL,等于不包含参数的整个请求路径:http://localhost:8080/Example/AServlet 。
㈧ 如何解决javarequest请求中有多个参数的问题
通过程序遍历http请求的所有参数放到hashmap中,用的时候方便了。
如果参数值有中文,那么需要在程序中添加filter转码,或者在下面程序里,对paramValue转码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map map = new HashMap();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
System.out.println("参数:" + paramName + "=" + paramValue);
map.put(paramName, paramValue);
}
}
}
Map map = new HashMap();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
System.out.println("参数:" + paramName + "=" + paramValue);
map.put(paramName, paramValue);
}
}
}
}
㈨ java中session和request的区别
request 指在一次请求的全过程中有效,即从http请求到服务器处理结束,返回响应的整个过程,存放在HttpServletRequest对象中。在这个过程中可以使用forward方式跳转多个jsp。在这些页面里你都可以使用这个变量。request是用户请求访问的当前组件,以及和当前web组件共享同一用户请求的web组件。如:被请求的jsp页面和该页面用<include>指令包含的页面以及<forward>标记包含的其它jsp页面;
Session是用户全局变量,在整个会话期间都有效。只要页面不关闭就一直有效(或者直到用户一直未活动导致会话过期,默认session过期时间为30分钟,或调用HttpSession的invalidate()方法)。存放在HttpSession对象中 ,同一个http会话中的web组件共享它。