forward action with Parameters JSP Example


We studied jsp:forward earlier, the forward action usage, importance and when to prefer over jsp:include. Now let us go for the same jsp:forward but with parameters. With this feature supported by JSP, one JSP file can send the client request to another JSP file to fulfill, with extra information known as parameters. These parameter values can be used by other JSP. Read further forward action with Parameters JSP.
Example on forward action with Parameters JSP

A small application is given with 2 files.


1. Server-side JSP, File Name: KnowResult.jsp

This file sets student marks as parameter to and is read by Process.jsp. This file calls Process.jsp with action.

2. Server-side JSP, File Name: Process.jsp
This file reads marks, set by KnowResult.jsp, using getParameter() method, evaluates the result and sent to client directly without involvement of KnowResult.jsp. If any data exists in KnowResult.jsp, it is not sent.

1. File Name: KnowResult.jsp


  Following is the student result. 

  
     
  

  Thank you.        

2. File Name: Process.jsp


  <%  
   int marks = Integer.parseInt(request.getParameter("stdMarks"));  // reads from jsp:param value
   if(marks >= 35)
     out.println("Student Passed"); 
   else
     out.println("Student Fialed"); 
  %>


Screenshot sent by Process.jsp

forward action with Parameters JSP

The code is simple and self explanatory.

See in the screenshot, the contents of KnowResult.jsp are not sent to client and not printed.

Leave a Comment

Your email address will not be published.