File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Calling a java file in a JSP file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Calling a java file in a JSP file" Watch "Calling a java file in a JSP file" New topic
Author

Calling a java file in a JSP file

Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68


Im using Tomcat 7. I dont know why am I getting the this error...
Here SHA1 is a java file in the same package as the JSP file.


Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 18 in the jsp file: /welcome.jsp
SHA1 cannot be resolved to a type
15: <h1> Hi, :)</h1>
16:
17: <%
18: SHA1 sh = new SHA1();
19: String SID = session.getId();
20: String HMAC_SID = sh.calcSHA1(SID);
21:

Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at LoginProcess.processRequest(LoginProcess.java:57)
at LoginProcess.doPost(LoginProcess.java:132)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Java code in a JSP is a big no-no since 2002. Why is this not being done in a servlet controller?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Gaurav Wadhwani wrote:Here SHA1 is a java file in the same package as the JSP file.


This doesn't make any sense. The JSP file isn't in a package. Or rather, when it's compiled it's in a package whose name you don't know.

And when you call it a "java file" that suggests to me that you haven't compiled it to a class, but just put the uncompiled Java code into the same folder as the JSP code.
thippe swamy
Greenhorn

Joined: Feb 04, 2012
Posts: 10

try instantiating the SHA1 in a servlet and set the object in some scope. Get this object in the jsp and use it.
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
Thank bear and pual for the responses :)

Thippe, i just did that.

So a servlet before welcome.jsp has this code



and welcome.jsp changed to this




I thought you should be passing objects in Attributes of session and later using them.




Now If i type cast then it would be creating an object of SHA1, which is a java code.
What I want to do is, use this object to access the function calcSHA1().
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
If the only purpose of the SHA object is to create an id to store in a cookie, why even remember it in session?

A JSP page should have NO java code in it. None at all.
This is all the java code you need.
Given that it is working with the response headers, I would say it would best be implemented as a Filter, with all requests to your web app going through it.




By the way, you do realise that the session id is normally passed as a cookie anyway?
What is it you are trying to accomplish by storing this extra value?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

If you don't want to mess with filters and servlets, simply create a custom tag.
Include your tld and embed the call to it.

I also concur with the other comments that Java scriplets <% %> are definitely no nos in todays world.
Unfortunately my current employer has over 10 years of them strewn in almost all jsps!



In the html:



...




It's been a while so syntax may be off, but I'm sure you can catch my drift.
Also you will need a tld xml file!

Pat.
Gaurav Wadhwani
Ranch Hand

Joined: Sep 21, 2010
Posts: 68
@Stefan : This is what Bear said which I wasnt aware. I had changed the code similar to what you suggested. And yes, i know about the SESSIONID cookie, but wanted to show it in some other way.

@William : The taglib idea is great. I would implement it sometime in my project.


Resolved :)

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a java file in a JSP file