使用request.setAttribute和get时获取空值 [英] Getting null values while using request.setAttribute and get

查看:445
本文介绍了使用request.setAttribute和get时获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个变量从Admin.java文件传递给index.jsp。我在Admin.java中打印它时得到的值。我需要将该值传递给另一个需要发送到index.jsp的变量。这个新变量获取空值。



Admin.java中的代码是

  public string static rest; 

protected void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException
{
SemanticSearch semsearch = new SemanticSearch(request.getSession());
semsearch.loadData(REALPATH + RDFDATASOURCEFILE1);

String res = semsearch.searchForUser(userName,password);
System.out.println(res传递的值是+ res);
request.setAttribute(rest,res);
System.out.println(休息的价值是+休息);
request.getRequestDispatcher(index.jsp)。forward(request,response);

if(res!= null)
{
request.getSession()。setAttribute(access,true);

System.out.println(Admin:doGet:login:true);
response.getWriter()。write(existnoadmin);
返回;
}

输出:

  res传递的值是C语言。 
rest的值为null。

根据堆栈溢出中提出的问题,我们需要在需要发送时使用转发或重定向值到jsp页面。但在我的情况下,我试图从函数返回值,所以我不知道我在上面的代码中尝试的方式是否正确。



index.jsp中的代码是:

  if(response ==existnoadmin)
{
警报(响应);
alert(admin.java获取的值是+ request.getAttribute(rest));
out.println(admin.java获取的值是+ request.getAttribute(rest));
alert(我们在index.jsp中完成了);
}

输出结果是我收到了existsnoadmin的警告框。



但是我无法在这里或在Admin.java中获得休息的价值。



什么是我在这里做的错误?请帮忙。



问候,



Archana。

解决方案

你说JSP中的代码是这样的:

  if(response) ==existsnoadmin)
{
alert(response);
alert(admin.java获取的值是+ request.getAttribute(rest));
out.println(admin.java获取的值是+ request.getAttribute(rest));
alert(我们在index.jsp中完成了);
}

我在理解这真正意味着什么时遇到了问题。



如果上面的代码是出现在scriptlet标签内的Java代码<% ... %> ,然后我不明白 alert(响应); 是如何显示任何内容的。实际上,它应该在JSP中给你一个编译错误。



另一方面,如果上面是嵌入在JSP生成的页面中的Javascript代码,那么




  • request.getAttribute(rest)不可能work ...因为在Web浏览器中不存在您设置属性的请求对象,并且


  • out.println (...)无法正常工作,因为Web浏览器中不存在JspWriter。




<你没有准确地转录JSP摘录,或者你的Java和/或Javascript没有意义。






根据您的评论,我认为您需要以下内容。

  if(response ==existnoadmin)
{
alert(response);
alert('admin.java获得的值是'+
'<%request.getAttribute(rest)%>');
// admin.java获取的值是<%request.getAttribute(rest)%>
}

或者如果你想摆脱潦草的东西......

  if(response ==existnoadmin)
{
alert(response);
alert('admin.java获取的值是'+
'$ {requestScope.rest}');
// admin.java获得的值是$ { requestScope.rest}
}

如果你想要我已经变成的东西一个 // JS注释在页面上可见,您将其移动到HTML的某些内容部分。目前它(我假设)在< script> 元素内,因此不会显示。



所有这些黑魔法的关键是理解JSP的哪些部分由什么来看/评估:




  • JSP指令例如JSP编译器评估< @ import ...>

  • 在scriptlet标记内填充例如<%...%> ,EL表达式,例如 $ {...} 或JSTL标签,例如当JSP运行时,< c:out ... \> 会被评估。

  • 由...生成的任何内容在收到HTTP响应后,在用户的浏览器中显示/执行JSP(HTML内容,嵌入式Javascript)。







现在需要在admin.java中使用request.dispatcher .... forward命令。


您的主servlet可以执行以下两种操作之一。




  • 它可以使用请求调度程序将请求转发到JSP。如果它这样做,它可以通过设置请求属性转发其他值。


  • 它可以打开响应输出流并向其写入内容。




不应尝试同时执行这两项操作! (我不确定会发生什么,但可能会导致500内部错误。)


I need to pass a variable from Admin.java file to index.jsp. I get the value when I print it in Admin.java. I need to pass that value to another variable which needs to be sent to index.jsp. This new variable gets null value.

The code in Admin.java is

public string static rest;

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
{
    SemanticSearch semsearch = new SemanticSearch(request.getSession());
    semsearch.loadData(REALPATH + RDFDATASOURCEFILE1);

    String res=semsearch.searchForUser(userName, password);
    System.out.println("The value of res been passed is "+res);
    request.setAttribute("rest", res);
    System.out.println("The value of rest is "+rest);
    request.getRequestDispatcher("index.jsp").forward(request, response);

if(res != null)
 {
       request.getSession().setAttribute("access", true);

       System.out.println("Admin:doGet:login:true");
       response.getWriter().write("existsnoadmin");
       return;
}

Output:

The value of res been passed is C Language.
The value of rest is null.

As per the questions asked in stack overflow we need to use forward or redirect when we need to send the value to jsp page. But in my case, I am trying to return value from a function so I do not know whether the way I am trying to do in the above code is right or not.

The code in index.jsp is:

if(response=="existsnoadmin")
 {
    alert(response);
    alert("The value obtained by the admin.java is " +request.getAttribute("rest"));
    out.println("The value obtained by the admin.java is " +request.getAttribute("rest"));
    alert("we have done in index.jsp");
}

The output is that I am getting the alert box which says "existsnoadmin".

But I am not able to get the value of rest here nor in Admin.java.

What is the mistake I have done here? Please help.

Regards,

Archana.

解决方案

You say that the code in the JSP is this:

if(response=="existsnoadmin")
{
    alert(response);
    alert("The value obtained by the admin.java is " +request.getAttribute("rest"));
    out.println("The value obtained by the admin.java is " +request.getAttribute("rest"));
    alert("we have done in index.jsp");
}

I'm having problems understanding what this really means.

If the above code is Java code that appears inside scriptlet tags <% ... %>, then I don't understand how alert(response); is showing you anything. In fact, it should give you a compilation error in the JSP.

On the other hand, if the above is Javascript code that is embedded in the page that the JSP generates, then

  • request.getAttribute("rest") cannot possibly work ... because the request object that you set the attribute on does not exist in the web browser, and

  • out.println(...) cannot work because the JspWriter does not exist in the web browser.

Either you have not transcribed the JSP excerpt accurately, or your Java and/or Javascript doesn't make sense.


Based on your comment, I think you need the following.

if(response=="existsnoadmin")
{
    alert(response);
    alert('The value obtained by the admin.java is ' +
          '<% request.getAttribute("rest") %>');
    // The value obtained by the admin.java is <% request.getAttribute("rest") %>
}

Or if you want to get rid of the scriplet stuff ...

if(response=="existsnoadmin")
{
    alert(response);
    alert('The value obtained by the admin.java is ' +
          '${requestScope.rest"}');
    // The value obtained by the admin.java is ${requestScope.rest"}
}

If you want the stuff that I've turned into a // JS comment to be visible on the page, you been to move it to some content part of the HTML. Currently it is (I assume) inside a <script> element, and therefore won't be displayed.

The key to all of this black magic is understanding what parts of a JSP are seen/evaluated by what:

  • JSP directives e.g. <@ import ...> are evaluated by the JSP compiler.
  • Stuff inside scriptlet tags e.g. <% ... %>, EL expressions e.g. ${...} or JSTL tags e.g. <c:out ...\> gets evaluated when the JSP is "run".
  • Anything generated by the JSP (HTML content, embedded Javascript) is displayed / executed in the user's browser after the HTTP response has been received.

Now is it neccessary to use the request.dispatcher....forward command in admin.java.

Your primary servlet can do one of two things.

  • It can use the request dispatcher to forward the request to your JSP. If it does this it can forward additional values by setting request attributes.

  • It can open the response output stream and write stuff to it.

It should not attempt to do both! (I'm not sure exactly what will happen, but it is likely to result in a 500 Internal Error.)

这篇关于使用request.setAttribute和get时获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆