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

How to print the Java data from a JSP  RSS feed

 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, im new to JSP, i want to write a JSP that can read the exif infomation from JPEG,
1)how can i use the $path in <%!
2)how can i print the data from <%!
thank you & sorry for my poor english!

script︰

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lek Kin Wong wrote:1)how can i use the $path in <%!

$path is variable in page scope and can be used like normal Java variable access:

Lek Kin Wong wrote:2)how can i print the data from <%!

System.out.println won't work, try printing the data to the page itself, using out.print
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm genuinely curious: where do people get the idea of putting Java code into JSP pages? Are there still tutorials out there that advocate this? This has been considered bad design and bad style for at least half a decade. That's what MVC is all about.
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
Lek Kin Wong wrote:1)how can i use the $path in <%!

$path is variable in page scope and can be used like normal Java variable access:

Lek Kin Wong wrote:2)how can i print the data from <%!

System.out.println won't work, try printing the data to the page itself, using out.print

thank you very much, i will try is out later.
by the way, i posted this thread in many forums and get none reply, you just give me a big hand, thanks!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way, i posted this thread in many forums

That being the case, please be aware that we're asking people to BeForthrightWhenCrossPostingToOtherSites. It's the polite thing to do.
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm genuinely curious: where do people get the idea of putting Java code into JSP pages? Are there still tutorials out there that advocate this? This has been considered bad design and bad style for at least half a decade. That's what MVC is all about.

thank you for your advice,
however,
because im very new to the JSP,
and actually i know nothing about the JAVA programing,
i even dont know how to separate the JAVA code from a JSP,
make a JAR package or build a Class? how to mport a JAVA code into my JSP? i know nothing about this,
i tried to google around for this but i get nothing.
at the end i put the JAVA code into my JSP...
sorry for my innocent.
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
by the way, i posted this thread in many forums

That being the case, please be aware that we're asking people to BeForthrightWhenCrossPostingToOtherSites. It's the polite thing to do.

oh... okay, im sorry, i will be observed to BeForthrightWhenCrossPostingToOtherSites. many thanks for your remind.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article should get you started: http://www.javaranch.com/journal/200603/frontman.html
I'd also advise to work through a servlet/JSP introduction like the "Head First Servlets & JSP" book. Once you've become accustomed to bad habits, it's hard to break them.
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:This article should get you started: http://www.javaranch.com/journal/200603/frontman.html
I'd also advise to work through a servlet/JSP introduction like the "Head First Servlets & JSP" book. Once you've become accustomed to bad habits, it's hard to break them.

thank you for your link, and i like this sentence "if I can look back at code I had written previously and know that the code I am writing today shows significant improvement, it means that I am still learning and growing as a coder." i will work hard on JSP and JAVA~
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and welcome to JavaRanch
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
Lek Kin Wong wrote:1)how can i use the $path in <%!

$path is variable in page scope and can be used like normal Java variable access:

Lek Kin Wong wrote:2)how can i print the data from <%!

System.out.println won't work, try printing the data to the page itself, using out.print

hi, i tried to change the 26line to "File f = new File($path+"DSC_9268.JPG");" and using out.print,
but i get a error of "$path cannot be resolved",
and "out cannot be resolved",
here is my error report of tomcat:

An error occurred at line: 27 in the jsp file: /05_exif/exif.jsp
$path cannot be resolved
24: <%!
25: public static void main(String args[]) throws FileNotFoundException {
26: //File f = new File("D:/apache-tomcat-6.0.20/webapps/05_exif/DSC_9268.JPG");
27: File f = new File($path+"DSC_9268.JPG");
28: try {
29: ExifReader er = new ExifReader(f);
30: Metadata exif = er.extract();


An error occurred at line: 24 in the jsp file: /05_exif/exif.jsp
out cannot be resolved
21: </head>
22: <body>
23:
24: <%!
25: public static void main(String args[]) throws FileNotFoundException {
26: //File f = new File("D:/apache-tomcat-6.0.20/webapps/05_exif/DSC_9268.JPG");
27: File f = new File($path+"DSC_9268.JPG");

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I see that all the good advice to remove the Java code from the JSP is going to be ignored?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main, that's not what JSP are meant for. You really need to go through some JSP/Servlet tutorial. Here is free PDF, http://pdf.coreservlets.com/
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:public static void main, that's not what JSP are meant for. You really need to go through some JSP/Servlet tutorial. Here is free PDF, http://pdf.coreservlets.com/

thanks, this help me a lot
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:So I see that all the good advice to remove the Java code from the JSP is going to be ignored?

hi, i tried to put the JAVA code into a .java, made a .class and put the servlet into webapp/XXX/WEB-INF/classes,
but i get 404 error when i tried to access the servlet,
i tried to uncommented the invoker in /conf/web.xml, nothing change but still get 404 =[
however, if i put the servlet into webapp/root/WEB-INF/classes, everything go fine @@?
any advice is appreciated.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Advice #1: NO Java code in JSPs. None. Ever.

Advice #2: See Advice #1.
 
Lek Kin Wong
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Advice #1: NO Java code in JSPs. None. Ever.

Advice #2: See Advice #1.

I am tring my best to do that~
now I am tring to separate the Java code and the JSP code by using <jsp:useBean>,
I can set and get a variable with <jsp:setProperty> and <jsp:getProperty>,
however, I dont know how to run those "EXIF READER code" in JBean,
even easy as a Hello World in JBean:

no erorr but also nothing printed... how can I run the code in main?

 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're going off on the wrong track here.

The JSP should be used just for interacting with the browser/user. It should be used to get input from the user (via <form> and <input> elements), then used to display the results of the application's processing. That's all.

The point others are trying to make is you don't want to do any application logic in the JSP page.

Instead, place your application logic in a Servlet. You'll need to read-up on how to use Servlets. There are tutorials and even free books available on the Internet. Start here: http://www.coreservlets.com/ and here: http://pdf.coreservlets.com/. One thing about books on the JSP topic is that they'll show examples which include application logic in the JSP page. They do this just to make the example easier to get up and running (so they don't have to also create a Servlet) - ignore that. Even through it's possible, never place application logic inside the JSP page.

The basic premise is that you'll start with a JSP page which gathers necessary input from the user and does a POST to the Servlet.
The Servlet then performs all the necessary processing, including the parsing of the data you've been talking about, etc., then creates an object on the session to hold all the results that will be displayed to the user. This object can be a JavaBean.

The Servlet then forwards the request to a JSP page that will be used for the display of the data. The JSP page displays the data it retrieves from the session object (the JavaBean or whatever).

Note: In the normal running of things, you will never use a "main" method in either a Servlet or JavaBean. These method can be used when you unit test these components, but they are never called when used by the container.

Best Regards,
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this article will help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!