• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Problem passing data from HttpServlet to JSP

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a project that gathers data that I want to display
in a web browser. The project uses java and JSP on a linux
operating system:

        Linux roe 4.19.0-13-amd64 #1 SMP Debian 4.19.160-2 (2020-11-28) x86_64 GNU/Linux

I am running Apache Tomcat server 8.0.53 and java jdk 1.8.

I found the following web page, which I will call 'Ref_1':

       https://www.programmergate.com/pass-data-servlet-jsp

'Ref_1' gives a good description on how to move data from a java
HttpServlet controller to a JSP target page. My target page is
the file 'list.jsp'.

In the java controller, I have followed 'Ref_1' and added the code:

   

When I run this code in eclipse, I can verify that the request
object gets set with the attribute 'jja' set to 'jjaTest'.

My understanding is that the 'request' and 'response' objects are passed to
JSP page as implicit objects that are available as Expression Language (EL)
variables or by referring to the implied objects by name
as 'request' or 'response'.

In the file, 'list.jsp', I have added the following code, as suggested
by 'Ref_1':


   

When 'list.jsp' is displayed in the browser, the results are:

   Test is

   Test attribute is null

In the first output line, printing the EL variable ${jja} shows nothing. I assume this means
the value was not successfully passed from the controller. Or if
it was, it is not being read correctly.

In the second output line, request attribute prints a 'null' value.

Obviously, I'm doing something wrong. Does anyone see the error?

Jim A.
 
Marshal
Posts: 3506
496
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your code using a Tomcat Docker image (tomcat:8.5.63-jdk8-openjdk-buster), and it did seem to work (I did fix the path for the JSP though):



 
Jim J Anderson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@Ron

Thank for for testing this issue.

I have a few follow up questions:

I did fix the path for the JSP though



Do you mean you adjusted the path for the JSP in your own environment so that you could reproduce the case. In my own environment, the uri for list.jsp works and the page gets displayed. As far as I know, the request object passed to the JSP should be valid and should contain the correct value for the attribute 'jja'.


My next question assumes that you agree that my example should work. You used a different version of Tomcat. As a followup, I should test my example with other versions of Tomcat? Note: I will do this, but I am just trying confirm that your opinion is that my example should work.

Jim A.

P.S. I'm sorry for the delay, but I had to take my son to the hospital yesterday and could not get to the computer. He was released and is doing fine :)
 
Jim J Anderson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Update:

I ran my test case using Apache Tomcat 8.5.63 and got the same results as when I used 8.0.53.

Jim A.
 
Jim J Anderson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My current thinking is that this could be a problem with my environment. I included some of my environment in my initial post, and the only thing I can think of to add is the browser. I think the entire relevant environment is shown below. I omitted anything, feel free to ask:

Jim A.

my environment:

results of 'uname -a"  - Linux roe 4.19.0-13-amd64 #1 SMP Debian 4.19.160-2 (2020-11-28) x86_64 GNU/Linux
tomcat version: 8.0.53 or 8.5.63 (the test case fails with either version of tomcat)
browser; Firefox 86.0 - 64-bit
jdk1.8.0_261

 
Ron McLeod
Marshal
Posts: 3506
496
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim J Anderson wrote:In my own environment, the uri for list.jsp works and the page gets displayed.


The documentation for ServletContext#getRequestDispatcher says: The pathname must begin with a / and is interpreted as relative to the current context root. Use getContext to obtain a RequestDispatcher for resources in foreign contexts.

In your example, the path is just list.jsp (which is not according to the specification).  If I specify the path that you are trying, I get an error at run time:
java.lang.IllegalArgumentException: Path [list.jsp] does not start with a "/" character
   jr740176.HelloWorld.doGet(HelloWorld.java:17)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



It would be easier to help you troubleshoot your problem if you could provide more details:

1. Provide complete code (not just fragments) for the servlet and the the jsp files.

2. Specify the URL that you are entering in the browser.
   for my testing it is: http://localhost:8080/test/HelloWorld
   where test is the name of my war, and HelloWorld is the name of servlet used for URL pattern matching

3. Show the contents of the generated war file.
   Mine looks like this:
jar tvf test.war
    0 Thu Mar 11 08:11:08 PST 2021 META-INF/
   94 Thu Mar 11 08:11:08 PST 2021 META-INF/MANIFEST.MF
    0 Thu Mar 11 08:11:08 PST 2021 WEB-INF/
    0 Thu Mar 11 08:11:08 PST 2021 WEB-INF/classes/
    0 Thu Mar 11 08:11:08 PST 2021 WEB-INF/classes/jr740176/
 1318 Thu Mar 11 08:11:08 PST 2021 WEB-INF/classes/jr740176/HelloWorld.class
  151 Tue Mar 09 20:17:06 PST 2021 WEB-INF/list.jsp
 
Ron McLeod
Marshal
Posts: 3506
496
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For completeness - this is the POM that I am using with Maven to build the war:
 
Jim J Anderson
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My apology for the delay. This is VERY important to me and the delay was not for lack of interest. My son visited this weekend and not much work could be done.

Since Friday, I have been working to reduce my 'website' testcase to the smallest possible set of code that I could create. I have been removing code, removing code and removing code. And the test case has gotten very small. The process I'm going through is error prone. In general, the process has been remove 10 or 20 chucks of code, compile, release, test. If the test passes, then back up the current version. A number of times, the testcase broke and  I have had to go back to a previous working state and carefully work toward the next state.

Anyway, I am nearly to the bare minimum and BINGO, my test case data is being passed from the HttpServlet to the JSP as it is supposed to. A minor problem for now is that I am not sure what change or changes in this last step fixed the problem. I plan to go through the last step much slower and try to isolate where the problem actually is (was).

When I figure it out, I will document what caused my problem.

Jim
 
Marshal
Posts: 26537
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that's a good thing. You have some working code. Now, the process is to add bits of code to it until it breaks. When that happens, you know that the last bit of code you added was the problem, or at least part of the problem. So don't add big bits of code, just add the smallest bits you can add, one at a time with tests in between.
 
Jim J Anderson
Ranch Hand
Posts: 50
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alas, problem solved.

This was a step by step process of creating a simple test case that works and then adding code i. Evemtually, when I added a few lines and the 'response' object attribute failed, the cause was obvious and I called myself a true dummy.

The problem was basically my losing track of the flow control in the website flow.

The application makes an ajax call and has 3 java controllers which handle:
 1) form submissions which change the server data  (change controller)
 2) form submissions which do not change the server data (no change controller)
 3) ajax submissions (ajax controller)

I have been working on the ajax submission code forever and it basically works. But I was so focused on the ajax submission, that I lost site of the fact that after the submission, the change controller was being called. I was setting an attribute on my ajax request object and expecting it show up in my JSP code. This was wrong.

The flow is:
     A) user works on form
     B) user submits form
     C) on submission the ajax controller processes submitted user data
     D) flow returns to the form
     E) the form 'action' is called and flow goes to the change controller
     F) the change controller 'fowards' and displays the next user page which contains results from the form request

I was setting a request attribute in step 'C' and expecting it to show up as an attribute in step 'F'.

To fix the problem, I moved the code to set the request object from step 'C' to step 'E'.

I want to thank Ron McLeod for his attention to this and  his thought provoking comments. I was not visually responsive to his comments here, but behind the scene I was busy because of those comments. While preparing a test case for him to look at, I gradually worked through this myself. I doubt I would have taken this path without his help. His running my code snippet and telling me that it works forced me to question: How can this work for him, but not for me?

Jim A
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic