• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing values from JSP to Servlets  RSS feed

 
Kunal Harkare
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to pass values from JSP Page to Servlets but I am getting a NullPointerException

Here is my JSP code



Servlet




Error StackTrace


 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

There are two problems with your approach. For starters, you misunderstand how request attributes work. Those are only available during the current request - which means the JSP in the case of where you're using it. The form submission to the servlet triggers a new request that has no access to any attributes from previous requests (those are gone forever).

Secondly, you're confusing attributes and parameters (note that the code calls "setAttribute" but "getParameter"). The servlet can get at those two values as parameters if the JSP code creates them as hidden form fields.

Lastly, code like Class.forName("com.mysql.jdbc.Driver") belongs in the init method of the servlet -so it's executed only once-, not in any of the service methods.
 
Kuldip Shetty
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add hidden type form element in html and read the value in Servlet using request.getParameter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!