Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

import java file into jsp.  RSS feed

 
Hardik Patel p
Ranch Hand
Posts: 40
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I want to access variable of java file into jsp Page. So I tried to do this but it does not work.

Problem : when I am trying to access "getName" method of java class into jsp file it displaying error, i already imported "Ajaxmethod.java" file in to "success.jsp" I want to access "getName" method in to jsp file without creating object of class.

Ajaxmethod.java


success.jsp


Thank you
Hardik
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few comments, in no particular order.

1) Java code has no place in JSPs, you should learn to use JSTL and EL.

2) The import syntax you should be using is <%@ page import="a.b.Ajaxmethod" %>

3) getName is a method of the Ajaxmethod class - if you want to invoke it you need an instance of that class.

4) You can create a fresh instance using jsp:useBean, or use an existing one that your servlet code has set as request attribute.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66187
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should create the variable as a scoped variable in request scope using request.setAttribute() in the controller. For example:

request.setAttribute("name","Bear");

Then, after forwarding to the JSP, you can reference it with ${name}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!