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

How to Declare a Class and Instantiate an Object in JSP

 
Ashwini Pahuja
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to declare a Class and i want Instantiate a Object and assign some values to the attributes of the Object and then pass the Object to a Servlet. Can anyone please Help me out???
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I don't really understand what you are asking.
I want to declare a Class and i want Instantiate a Object and assign some values to the attributes of the Object
So far this just sounds like regular Java. Write the source code for your class, compile it, and place the resulting class file in WEB-INF/classes. Then you can create an instance in your JSP using something like <% MyThing thing = new MyThing(); %> and assign things to attributes by either setting public fields <% thing.name = "whatever"; %> or using "bean syntax" <% thing.setName("whatever"); %>
and then pass the Object to a Servlet
This is what puzzles me. A servlet is essentially just a handler for HTTP requests. It accepts a request, reads parameters etc., does some processing, and produces a resulting web page. You can't expect to pass an object to a servlet.
Can you explain a little more about what you want this "servlet" to do with your object once you have "passed" it from your JSP?
 
Ravinder S Edhan
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You can create a class and after compiling it can place in WEB-INF/classes folder and in jsp file u can create object of that class using following jsp tag
<jsp:useBean id="object_name" class="class_name" scope"session/page/application/request"/>
<jsp:setProperty name="object_name" property="*"/>
<% =object_name.get_method_from_ur_class_file() %>
Check List
1. ur id and name should match
Bye
 
vijaykumar shah
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello..
Frank Carver is very right and his solution is very feasible rather than creating a bean just for nothing ...
or would rather suggest to elaborate your question that would be more feasible to be answered....
any java programmer knows to make a class object and instantiate the variable and use the methods of the class ...
its very simple and jsp is not other thing than java its also a java code
for more queries..always welcome
vijay shah
v.shah@portlog.com
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic