Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

data from servlet to jsp

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner in programming with servlets and jsp. I am creating a servlet and in the servlet I am creating an attribute and sending its value to a jsp page.
The attribute's value is then displayed on the jsp page. However, when displaying the value, null is being displayed and I cannot understand why.

below is the code for the servlet and the jsp:

ServletTest.java



index.jsp

 
Bartender
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which one is executed first, servlet or jsp? Another point, avoid using java code in jsp. Use JSTL/EL or custom tags etc.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attributes and Parameters are NOT the same thing.

Parameters are extracted from the user's request - Attributes can be attached to a request by your code.

Your code is not allowed to add Parameters to a request, the map of parameters is immutable.

See the JavaDocs for javax.servlet.ServletRequest

Bill
 
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your form action is incorrect. It should be "${pageContext.request.contextPath}/ServletTest". Without those {}, the $ only applies to pageContext, and the form action would be the page context's String representation followed by ".request.contextPath/ServletTest".
 
Beth Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies. I modified the getAttribute and setAttribute methods, however with the "${pageContext.request.contextPath}/ServletTest" I am getting an error telling me that there is a whitespace in path and I cannot understand why.

index.jsp



ServletTest.java




 
Rob Spoor
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the name of your web application?
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scriptlets are dead. Use the EL. To access the value of a scoped variable named "hello" you would use ${hello}.
 
Beth Smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies, it worked now
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic