Using an imported class in a JSP file
When you import a class you wrote into a JSP file, is there a way to submit values from forms to the entire class, or can you only use the methods within the class??
When you import a class you wrote into a JSP file, is
there a way to submit values from forms to the entire
class, or can you only use the methods within the
class??Yes, there is a way in JSP. You can use,
<jsp:useBean id="your_bean_id" scope="page or request etc" class="your_class">
<jsp:setProperty name="your_bean_id" property="*"/>
</jsp:useBean>
So, <jsp:setProperty> sets all the values inside your request object into the bean, if the class is having the properties (variables) with same name as of request object's parameters. You must have coded set method for all the properties whatever you want your jsp to set the values in the bean.
if your request is .../yourjsp?name=xxxx&age=25
then <jsp:setProperty> method will set the name and age inside bean if you have,
public class YourBean {
String name;
int age;
public void setName(String name) {
this.name=name;
public void setAge(int age) {
this.age=age;
Hope this helps.
Sudha
Similar Messages
-
Can't find the imported java class when run .jsp file
Hi,
I want to run a jsp web application using tomcat. I put all my .jsp files under "TOMCAT_HOME/webapps/junmin" directory, and put all .class files under "TOMCAT_HOME/webapps/junmin/WEB-INF/classes" directory. I set up TOMCAT_HOME AND JAVA_HOME already. But when I type in the address like below:
http://db1.acad.emich.edu:8080/junmin/login.jsp
The IE will give me error msg indicating that it can't find all the .class files which I imported them in login.jsp file.
So, does anyone know that if I miss some configuration in order to let the .jsp file recognize the .class files. Thanks a lot!
Sincerely, JunminHa, this might be the reason.
Can I add sth like this:
<Context path="/junmin" docbase="junmin" debug=0 reloadable=true>
</Context>
But in my local computer, I didn't add this <Context> either. It works well.
Thanks a lot! I will ask the administrator to try this.
And will write down the reason.
Sincerely, Junmin -
Re: Using simple java class in an .war file.
Hi All,
I have a simple question,
Can I access an simple java class from a jsp file that I have written by putting it in the "classes" folder?? What entry should go in the web.xml file?
Here is my directory structure:
sample.war
|
----test.jsp <file>
|
----WEB-INF <folder>
| |
| ----classes<folder>
| | |
| | ----simpleJavaClass.class<file>
| |
| ----lib<folder>
| |
| ----web.xml
I want to access the simpleJavaClass.class from the jsp test.jsp.
Please suggest.
Thanks and regards
AyusmanIt is possible to access the class in u r classes folder from u r jsp file.
For example
<%@ page import="com.test.service.PricingSolution,
com.test.core.PhoneNumber
%>
<% PricingSolution solution = (PricingSolution)session.getAttribute("solution"); %>
This is a simple scriplet example. You do the same using jstl tags also.
--Shinoy -
Importing class in a jsp using Tomcat
Hello All,
Using Tomcat:
I have class here:
examples/WEB-INF/classes/ package/myclass.class
I have a .jsp here:
examples/JSPFolder/myjspfile.jsp
I want to import the class into the jsp.
I have tried the following combinations of importy lines based on previous posts on these boards and each have failed with same error:
Unable to load class for JSP
<%@ page import="myclass" %>
<%@ page import="myclass.*" %>
<%@ page import="package.myclass" %>
<%@ page import="package.myclass.*" %>
<%@ page import="WEB-INF/classes/package/myclass%>
I have seen a number of posts for this same problem, and have tried those solutions as well, but none work. I am sure that this is simply a problem of finding the right directory, as I can successfully run the .jsp file iwhen I remove the import line.
Can someone please help?There is a folder for shared packages
tomcat_root\lib; or
tomcat_root\common
also most versions have a lib or common folder for each webapp for packages that are not to be shared.
tomcat_root\webapps\appname\web-inf\lib
Agreed that it is strange to allow servlets to import from the classes foler but not JSPs. It should make no difference but it is convension to place single classes in the same folder as the app's directory and packages (especially jar's) in the lib directory. -
Unable to import a java class in a jsp file
Hi,
I am trying to import a java class in my jsp.
------------------jsp---------------
<jsp:useBean id="form" class="com.company.portlets.searchApps.object.SearchOBJ" scope="request" />
<jsp:setProperty name="form" property="*" />
<input type="TEXT" name="projectID" value='<%= form.getProjectID() %>'>
When I run the above JSP, it says that class 'com.company.portlets.searchApps.object.SearchOBJ' cannot be found.
Why?
My project structure looks like this:
Project
+Application Sources
++com.company.portlets.searchApps.object
+++SearchOBJ.java
+Web Content
++htdocs
+++searchappsportlet
++++SearchAppsPortletShowPage.jsp
++WEB-INF
+++index.jsp
+Resources
++SearchApps.deploy
Please advice...thanks!The way that JDeveloper organized the files was like this:
C:\jdev1012\jdev\mywork\WksSearchApps\Project\classes\com\company\portlets\searchAPPS\object\SearchOBJ.class
I am assuming that JDeveloper puts the file where they are suppose to be...if not, how do I change that?
thanks,
hussain -
Invoking a method from a java class to a jsp file
Maybe i'm going through a very basic error, but as i've looked for in some forums and nothing found decided to post. So let's go to the problem.
Well, i have a form in a jsp file, it's basicaly html, but as i need some select boxes populated with data from the database i decided to create some methods in java classes. This is the point.
My method receive some attributes, like the table and colums names, the name of the select box and the HttpServletResponse object.
So after making all the database process. The method is supposed to write the result through the out.println(); object
It goes everything all right except that the method seems to be called first than the html code coz' my select boxes comes above the head in the begin of the page and was supposed to come in the middle.
part of the code:
package pacote;
import java.packages.*
public class Class{
public void metodo(HttpServletResponse response, String tableName, String column name, String selectName){
PrintWriter out = response.getWriter();
openConnection();
//After making the query stuff
//caso n�o haja nada na consulta
if(rst == null)
out.println("N�o h� registros.");
else{
out.println( "<select name='" +SelectName + "' id='" +SelectName + "' class=\"input_ob\" >");
out.println( "<option value=''>Escolha: "+objeto+"</option>");
//fill the select body with options; A while that sweap the resultSet and use the out object to write the results
getSelectBody(out,rst, selectValue);
}This method is called this way:
<tr valign="baseline">
<td nowrap align="right">Grupo</td>
<jsp:useBean id="m" scope="page" class="pacote.Class" />
<td>
<%
try{
m.metodo(response, "tabela","grupo","id_grupo);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
%></td>I'd like to know what i'm missing here. why the answer doesn't go in the place It's supposed to go.
any answer will be greatly apreciatedThe issue is caused because you are using the response to get the writer directly.
The JSP has gotten the same writer, but has wrapped it in a JSPWriter class. That class has got a buffer.
So it writes the beginning html to the JSPWriter buffer.
You write your stuff to the response writer - gets sent to the output.
It writes more stuff to the JSPWriter buffer, then flushes that buffer - at which point all the text written into the JSP writer now gets added to the response.
You see how your code is coming out first?
Solution - instead of getting the writer from the response, pass the writer into your method (maybe instead of response if you don't use response for anything else)
public void metodo(Writer jspWriter, String tableName, String column name, String selectName){
PrintWriter out = new PrintWriter(jspWriter);
...That means that you will be writing to the same writer, and the text should come out in sequence.
Cheers,
evnafets -
Hi,
I'm trying to get a jsp working. This is part of the code of test.jsp:
<%@page import="First, Second" %>
<HTML>
<HEAD>
<TITLE>JSP Example 3</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFCC">
<CENTER>
<H2>Date and Time</H2>
First.hello() is: <% First.hello(); %>
Second.hi() is: <% Second.hi(); %>
</CENTER>
</BODY>
</HTML>This is the output of the compilation in Tomcat:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 6 in the generated java file
The import First cannot be resolved
An error occurred at line: 7 in the generated java file
The import Second cannot be resolved
An error occurred at line: 9 in the jsp file: /date3.jsp
First cannot be resolved
6: <BODY BGCOLOR="#FFFFCC">
7: <CENTER>
8: <H2>Date and Time</H2>
9: First.hello() is: <% First.hello(); %>
10: Second.hi() is: <% Second.hi(); %>
11: </CENTER>
12: </BODY>First.class and Second.class are the two files who share the directory with test.jsp
I'm flabergasted with this error. As I am using very basic code for this example you would think it works (or is this error one of the NullPointerException kind?).
Abel
Edited by: Abel on Dec 12, 2007 6:21 AM
Changed to a more basic type of exampleI found the answer myself ~:-).
Add the files you want to import to a jar file
* To be more precise, in the directory I have the jsp, I created first a directory WEB-INF, and in that directory created a directory lib, and finally created a directory test. I moved the two Java files to the test directory. I changed the Java files (added a package statement). So the configuration is now:
<%@page import="test.*" %>
<HTML>
<HEAD>
<TITLE>JSP Example 3</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFCC">
<CENTER>
<H2>Date and Time</H2>
<%
First first = new First();
out.println ("First says: " + first.hello());
%>
</CENTER>
</BODY>
</HTML>And the two java files:
package test;
public class First {
public String hello() {
return ("Hello from First");
package test;
public class Second {
public String hi() {
return ("Hi from Second");
}And finally I added the two classes I got after compilation to a jar file in the WEB-INF\lib directory:
jar -cvf test.jar test\First.class test\Second.classI just explain what I did wrong, as I saw many comparable posts without an answer. And this basic problem can keep you away from the real work.
Abel. -
I have created a class file called BindListener.java and put it in my root directory in my web module. I cannot however get the import statement for the jsp right, my root directory is called JBuilder. I have tried putting it in the WEB-INF classes folder also but still when I try to execute thr jsp it dosent work.
There is no package declared in the classs file and it does not extend any other class, however it implements the HttpSessionBindingListener interface.
I can call servlets from my JSP in the WEB-INF classes dir using /JBuilder/ServletName
I am mondo confused please helpI have created a class file called BindListener.java
and put it in my root directory in my web module. I
cannot however get the import statement for the jsp
right, my root directory is called JBuilder. I have
tried putting it in the WEB-INF classes folder also
but still when I try to execute thr jsp it dosent
work.
There is no package declared in the classs file and
it does not extend any other class, however it
implements the HttpSessionBindingListener interface.You have to put the class in a package, then put the .class file in a package directory under WEB-INF/classes. So for example:
package beans;
public class BindListener ...
//goes into this directory structure
<APP_ROOT>/
*.jsp
WEB-INF/
web.xml
classes/
beans/
BindListener.class
//And is imported into your JSP using one of the following
// -- EITHER --
<%@ page import="beans.BindListener" %>
<%
BindListener bl = new BindListener();
%>
// -- OR --
<jsp:useBean id="bl" class="beans.BindingListener"/>>
I can call servlets from my JSP in the WEB-INF
classes dir using /JBuilder/ServletName
I am mondo confused please help -
Importing classes that implement jsp tags
I was making a custom JSP tag library. The tag functionality was implemented in
a class called, lets cay ClassA. I made the tld file and put it under the WEB-INF
directory. The class which implemented the functionality was placed under WEB-INF/classes
directory. I had the imported the tag library using the taglib directive. I was
getting an error which said "cannot resolve symbol". But when I in the JSP file
I imported the class file which implemented the taglib functionality the error
vanished. Is it necessary to import the class files even if the taglib is imported.
The documentation does not say so. Or is there some configuration I have to make.
I think was a side effect of the .jsp changing redeploys the web app in 6.0.
When the web app was redeployed your directory structure was reread and thus
it found your .tld.
Sam
"bbaby" <[email protected]> wrote in message
news:3b422db7$[email protected]..
>
> I was making a custom JSP tag library. The tag functionality was
implemented in
> a class called, lets cay ClassA. I made the tld file and put it under the
WEB-INF
> directory. The class which implemented the functionality was placed under
WEB-INF/classes
> directory. I had the imported the tag library using the taglib directive.
I was
> getting an error which said "cannot resolve symbol". But when I in the JSP
file
> I imported the class file which implemented the taglib functionality the
error
> vanished. Is it necessary to import the class files even if the taglib is
imported.
> The documentation does not say so. Or is there some configuration I have
to make.
>
>
-
How to use self-defined class in a jsp page
Hello:
I defined a class for displaying purpose, which is in the different folder(WEB-INF/classes) from my JSP page.
In my JSP page, I want to create an instance of my class and display something, while I get the following error: "
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 12 in the jsp file: /htmls/templateIntro.jsp
Generated servlet error:
[javac] Compiling 1 source file
/home/jiao/jsp_webserver/tomcat/work/Standalone/localhost/syllabus/htmls/templateIntro_jsp.java:72: cannot resolve symbol
symbol : class Display
location: class org.apache.jsp.templateIntro_jsp
Display display=new Display(out);"
How can I solve this problem? Should I redefine my classpath or there are some other actions I should take.
T.I.A.
Oriental SpiritHere is what I have done:
1) in Display.java, add one line "package tools;" on the top, and compile it to
create Display.class.
2) create a new folder at
/home/jiao/jsp_webserver/tomcat/webapps/syllabus/WEB-INF/classes
named tools and then put the Display.class into the newly created folder.
3) in my jsp page, add " import="tools.*"
But it still doesn't work.
Any ideas? T.I.A. -
How to reference a DatabaseHandler servlet class from a jsp file in Tomcat
Trying to create a database connection in the jsp file in webapps\project folder by referencing the DatabaseHandler class in webapps\project\WEB-INF\classes.
Here is the code in the jsp to make the connection:
DatabaseHandler db = new DatabaseHandler() ;
String sql = "SELECT password FROM cms_users WHERE user =" + userName ;
java.sql.ResultSet rs = db . selectQuery( sql ) ;
Here is the DatabaseHandler class:
import java.sql.*;
public class DatabaseHandler
// instance variables - replace the example below with your own
private Connection connection;
* Constructor for objects of class DatabaseHandler
public DatabaseHandler()
String url = "jdbc:odbc:javadatabase";
// Load the driver to allow connection to the database
try
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connection = DriverManager.getConnection( url );
catch ( ClassNotFoundException cnfex )
System.err.println( "Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
catch ( SQLException sqlex )
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
public ResultSet selectQuery( String query )
Statement statement;
ResultSet resultSet = null ;
try
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
catch ( SQLException sqlex )
sqlex.printStackTrace();
System . exit( 1 ) ;
return resultSet ;
Here is the error i am getting when i try to run the jsp script:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 2 in the jsp file: /ValidateLogon.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\cms\ValidateLogon_jsp.java:47: cannot find symbol
symbol : class DatabaseHandler
location: class org.apache.jsp.ValidateLogon_jsp
DatabaseHandler db = new DatabaseHandler() ;
^Just like in the class file you need to import any classes that you want to access in the JSP.
http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html -
Calling a java class in a JSP file
Hello,
I have a jsp file in which I call a java class.
I work with Tomcat 4.0.1
If I modify the java class and then I execute my JSP, it still be the old version of my class that is executed.
I don't understand why since if I creates a main function and I execute it, it works correctly!
Do you know what is the problem?
Thanksi also have this problem.when i modify jsp file,it didn't execute inmediately. Then i restart tomcat,but problem also exist.
a few minute age,i execute jsp file again,it can appear the modified jsp.i don't down why
i work tomcat 5.0 windowxp -
How do I import classes in a jar file?
I have the following directory structure, each directory is a seperate package of the same name as the directory
RPGUniversal
RPGMap
RPGCharacter
RPGCharacter is not done yet so I am ignoring that package for now.
RPGCharacter and RPGMap both depends on classes inside the RPGUniversal Package, but are independant of eachother and RPGUniversal is completely independant.
I want to place each package in it's own .jar file for use as a library in later programs.
I placed all the compiled classes for RPGUniversal inside the proper directory structure, I then used this command to place it in a jar:
jar -cf ./RPGUniversal.jar ./RPGUniversal/*
I then tested the contents with this command:
exodist@Abydos:/stuff/JProject/test$ jar -tf RPGUniversal.jar
META-INF/
META-INF/MANIFEST.MF
RPGUniversal/ADTs/
RPGUniversal/ADTs/t1.jpg
RPGUniversal/ADTs/t2.jpg
RPGUniversal/ADTs/t3.jpg
RPGUniversal/ADTs/t4.jpg
RPGUniversal/ADTs/Coordinates.class
RPGUniversal/ADTs/RPGImage.class
RPGUniversal/ADTs/FrameSet.class
RPGUniversal/ADTs/ImageTester.class
RPGUniversal/Exceptions/
RPGUniversal/Interfaces/
RPGUniversal/Interfaces/RPGCharacter.class
RPGUniversal/Interfaces/RPGItem.class
RPGUniversal/Interfaces/RPGObject.class
RPGUniversal/Interfaces/Trackable.class
RPGUniversal/Interfaces/Tracker.class
RPGUniversal/Interfaces/RPGMapComponent.class
RPGUniversal/Interfaces/RPGMap.class
so far all good, next I delete the RPGUniversal directory leaving the only file in the current directory RPGUniversal.jar
next I create the RPGMap directory and give it the proper .java files and directory tree.
I then run a script to compile every .java file
for i in `find ./ -name "*.java"`; do javac "$i"; done
and I get a ton of dependancy errors:
exodist@Abydos:/stuff/JProject/test$ for i in `find ./ -name "*.java"`; do javac "$i"; done
./RPGMap/BaseClasses/StaticMap.java:13: package RPGUniversal.Interfaces does not exist
import RPGUniversal.Interfaces.*;
^
./RPGMap/BaseClasses/StaticMap.java:20: cannot resolve symbol
symbol : class RPGMap
location: class RPGMap.BaseClasses.StaticMap
public abstract class StaticMap implements RPGMap
^
./RPGMap/ComponentClasses/MapTile.java:9: package RPGUniversal.Interfaces does not exist
import RPGUniversal.Interfaces.*;
^
./RPGMap/ComponentClasses/MapTile.java:10: package RPGUniversal.ADTs does not exist
import RPGUniversal.ADTs.*;
^
./RPGMap/ComponentClasses/MapTile.java:16: cannot resolve symbol
symbol : class RPGObject
location: class RPGMap.ComponentClasses.MapTile
public class MapTile implements RPGObject
^
./RPGMap/ComponentClasses/MapTile.java:19: cannot resolve symbol
symbol : class RPGImage
location: class RPGMap.ComponentClasses.MapTile
private RPGImage MyImage;
^
./RPGMap/ComponentClasses/MapTile.java:31: cannot resolve symbol
symbol : class Coordinates
location: class RPGMap.ComponentClasses.MapTile
public boolean CheckForCollision(Coordinates C)
....the list goes on........
how do I tell it to look inside the RPGUniversal.jar file to find the required classes?
here is a directory tree so you can see how it is set up:
ls -R1
RPGMap/
RPGUniversal.jar
./RPGMap:
BaseClasses/
ComponentClasses/
ExternalInterfaces/
HigherClasses/
InternalInterfaces/
./RPGMap/BaseClasses:
StaticMap.java
./RPGMap/ComponentClasses:
MapTable.java
MapTile.java
TableList.java
TileSet.java
./RPGMap/ExternalInterfaces:
./RPGMap/HigherClasses:
DynamicMap.java
./RPGMap/InternalInterfaces:The JAR file needs to be in the compliers classpath : javac -classpath "$CLASSPATH:RPGUniversal.jar" "$i"
-
How to use class in JSP file?
Hello All,
I am new to Jsp.
In Jsp file,I used class of myself.
Why,the Constractor seems not working?
Thanks in advance.Hi,
You can import the class in a jsp file.
Use <% page import = "">
and call that class.
If u want to import a funtion in your class, then u can insert that into scriplets like <%! ..%>
Hope u got that!
Artz -
I have written a library of classes for use in a standard application. In a completely separate project I need to use these classes in a JSP page. Under %tomcat/webapps/myproject is the JSP file. Where do I place the "normal" class files and how do I reference them in the JSP page? Do I need to import them or add them to the classpath and should they be pre-compiled ?
All help most appreciated
Thanks
BobWhere do I place the "normal" class files and how do
I reference them in the JSP page?Just like you use any other class in a JSP.
Do I need to import them or add them to the classpath and should they be pre-compiled ?Yes, yes and yes. The first thing is something you need to do for your stuff to compile (technically, you don't need to do it, but it's better than always using the fully qualified class names in the code). The second is something the servlet container will do for you when you deploy your WAR. The this is something you need to do with classes anyway. You don't need to do it for JSPs because the container will do it later, but that's all the difference there is.
Maybe you are looking for
-
Flash games on FaceBook don't work - Ubuntu 11.10 32 bit + Firefox 7.0.1
Hello, I installed today the FlashPlugin update for Ubuntu 11.10 (32 bit version). After the update all FaceBook games stoped working in Firefox 7.0.1. They still work with Chrome (version 15.0.874.120). System Information: AMD Semprom 140 3.7 GB of
-
HI, I have couple of queries on Web Analysis which are as follows: 1) Does the "Subset of UDA" option works for Multi-level combo box (Its not working in my report for time dimension)? 2) Is it possible to use "Keep Only" option on multiple columns a
-
Error FF 711 raised during posting of items in FI-AP
Hello Friends, we are getting error message FF 711 when trying to post from the ICM module into FI-AP using the BAPI_ACC_INVOICE_POST funcation Module. We have both service tax and withholding tax calculation switched on and everythings seems to work
-
Archive log mode in 3 node rac database
Sir I am using oracle 10.2.0.1 and created 3 node cluster setup on.OS i am using linux 4 I am too much confuse in setting up this cluster in archive log mode bec why we need to false cluater_database parameter to setup cluster in archive log mode. I
-
I have forgotten my security questions for my apple id so I can't make any purchases with my gift card that I recently got. To reset them the apple site tells me to go into Password and Security but to get into that you need to answer security questi