How to Use Request Object in Java

Java Server Pages (JSPs) are designed to run from a Web server and give Java programmers the ability to dynamically create Web pages in response to client or other computer requests. JSPs use implicit objects, which are available to be requested via a JSP Container on each dynamically generated Web page through a Java Servlet or Java Bean. Implicit objects can be accessed as predefined variables by the programmer via script in a Servlet. The objects provide information that you may find useful for Web page display modifications, such as providing table properties, cookies stored on a clients computer from past website visits and information on what Web browser and operating system the client computer is using.

Instructions

    • 1

      Open a new Java Server Page project in your integrated development environment (IDE). The request method is a common Object request used by JSP programmers to obtain table information from a Web page. This can be useful for your project if you need to dynamically change the information that is displayed on a Web page table.

    • 2

      Enter the following code to define an HTML Web page table:

      <table border=1 cellspacing=0 cellpadding=2>
      </table>

    • 3

      Add the following code to dynamically generate a listing of String headers to apply to the table after the opening table tag:

      <%
      String strHeaderName = "";
      For(Enumeration e = request.getHeaderNames();
      e.hasMoreElements(); )
      {
      strHeaderName = (String)e.nextElement();
      %>

    • 4

      Use the request method to request the table header information dynamically generated in Step 3 to add to the title of the table columns by using the following Java code before the closing table tag:

      <tr>
      <td><%= strHeaderName %></td>
      <td><%=request.getHeader(strHeaderName)%></td>
      </tr>
      <%
      }
      %>

    • 5

      Define an array to store implicitly requested cookie objects on the client's Web browser. This is a common task for JSP authors to conduct to dynamically deliver tailored content to the user. Use the following code to define the array:

      Cookie[] myJSPCookies = request.getCookies();

    • 6

      Create a programming loop to access the information stored in the array that contains the cookie information. To do this, add the following Java code:

      For (int j=0; j < myJSPCookies.length; j++)
      {
      }

    • 7

      Output the information stored in the cookie to the Web page you are creating. Note this is for testing purposes. Normally you will use this information for other website operations with your JSP pages. Use the following code inside the "for" loop defined in Step 3 to output the cookie information:

      out.print(myJSPCookies[j].getName() + " ");
      out.print(myJSPCookies[j].getValue() +"<BR>");

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured