Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a java file in a JSP file

 
Gaurav Wadhwani
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java code in a JSP is a big no-no since 2002. Why is this not being done in a servlet controller?
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Chrome Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1746
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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 :)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic