[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

forwarding value that was created in servlet to JSP



Hi!

I am trying to forward value that was created in servlet to JSP. How do I do it?
(This is what I did and I know “primeNum” contain prime # and I am wondering how to pass it to jsp page.)

public class PrimesOne extends HttpServlet {

  private int defalutSize = 30;
  BigInteger primeNum = BigInteger.ZERO;


  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {

    String primeSize = request.getParameter("primeSize");

     int length = 0;

    if(primeSize!=null&&primeSize.length()>0){
       length = Integer.parseInt(primeSize);
       primeNum = Primes.nextPrime(Primes.random(length));
    }else{
        primeNum = Primes.nextPrime(Primes.random(defalutSize));
     }


     getServletContext().setAttribute("primeNum", primeNum);

      RequestDispatcher dispatcher =
      getServletContext().getRequestDispatcher("/hw9/PrimesOne.jsp");

     dispatcher.forward(request, response);
++++++++++++++
<%
String primeNum = (String)request.getAttribute("primeNum");
%>
<BODY>
<CENTER>

<H1> Prime number :</H1><br>
<I><%= primeNum %></I>


thanks,
0
dkim18
Asked:
dkim18
  • 10
  • 7
1 Solution
 
koppchaCommented:
<%= request.getParameter("primeNum") %>
0
 
dkim18Author Commented:
koppcha ,

I am very sorry, when I tested it, it worked. Now, it doesn't work even though it is same code. So I assume there is something wrong, but it worked when I tested it somehow. Can you please take a look again? I changed many different ways, but it doesn't seem to work...

package hw9;

import coreservlets.ServletUtilities;
import moreservlets.Primes;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.math.BigInteger;



public class PrimesOne extends HttpServlet {

  private int defalutSize = 30;
  BigInteger primeNum = BigInteger.ZERO;


  public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
       throws ServletException, IOException {

     String primeSize = request.getParameter("primeSize");

      int length = 0;

     if(primeSize!=null&&primeSize.length()>0){
        length = Integer.parseInt(primeSize);
        primeNum = Primes.nextPrime(Primes.random(length));
     }else{
         primeNum = Primes.nextPrime(Primes.random(defalutSize));
      }


      getServletContext().setAttribute("primeNum", primeNum);

       RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/hw9/PrimesOne.jsp");

     dispatcher.forward(request, response);

  }


}
++++++++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> HW9 -#1</TITLE>

</HEAD>

<BODY>
<CENTER>

<H1> Prime number :</H1><br>
<I> <%= request.getParameter("primeNum") %></I>


</CENTER>
</BODY>
</HTML>
0
 
koppchaCommented:
change this part in the servlet
> getServletContext().setAttribute("primeNum", primeNum);

to
request.setAttribute("primeNum", primeNum);

Unless you really want application scope otherwise this should work
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
koppchaCommented:
Let me know what happened?
0
 
dkim18Author Commented:
I already tired that and it doesn't work. It keeps showing 'null.'...
0
 
koppchaCommented:
How do not know how this got compiled for you..it should have asked you to catch NumberFormatException.Please keep that part in try and catch block otherwise it wont parse may be you are not puttin any value at all in the request.

if(primeSize!=null&&primeSize.length()>0){
        length = Integer.parseInt(primeSize);//This part with out try catch doesn't work
        primeNum = Primes.nextPrime(Primes.random(length));
     }else{
         primeNum = Primes.nextPrime(Primes.random(defalutSize));
      }
0
 
dkim18Author Commented:
It compiled without try and catch block. Anyway, I put it in try and catch block, but didn't help.
0
 
koppchaCommented:
ok let us do a simple test ...
in the servlet keep

request.setAttribute("name","dkim18");

compile it  and in JSP
you keep
<%= request.getParameter("name") %>

try to redploy otherwise in some appservers you can not see the effect
0
 
dkim18Author Commented:
hm...just keeps showing null...
I am testing in school server.
0
 
koppchaCommented:
hmmm you can see now it is not being forwarded correctly.
0
 
koppchaCommented:
Just give ("/PrimesOne.jsp");
and see whats happening?
0
 
dkim18Author Commented:
I just insert this in servlet and shows result.

    out.println(ServletUtilities.headWithTitle("HW9 -#1") +
                       "<BODY>\n" +
                       "<CENTER>\n" +
                       " <H1>Prime number:</H1> \n" +

                         primeNum +"</CENTER>\n"+
                   "</BODY></HTML>");

but, I need to do it in jsp.
So, it does show changes...
0
 
koppchaCommented:
ok just forward it correctly did you try what i have said?
0
 
dkim18Author Commented:
PrimesOne.jsp is under ~dkim18/hw9/ dir.
Yes, I tried what you have said...
0
 
koppchaCommented:
hmmm
use the same in servlet
 getServletContext().setAttribute("primeNum", primeNum);


in JSP keep this
<%= application.getAttribute("primeNum") %>
0
 
dkim18Author Commented:
it keeps showing null.

If I do like this, it shows the number that I entered.

servlet
request.setAttribute("primeNum", primeNum);

jsp
<%= request.getParameter("name") %>
0
 
koppchaCommented:
that is strange....
Did you solve your problem?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now