Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't this work 1

Status
Not open for further replies.

SalShaikh

Programmer
May 15, 2002
16
US
Can anyone tell me why the above code doesn't work and give me the following error and the bottom code does.

Thanks


///////////////////////////////////
NOT WORKING CODE
///////////////////////////////////

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<%@ page language=&quot;java&quot; %>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%! String sal = request.getParameter(&quot;username&quot;); %>
<%= sal%><br/>
</body>
</html>

///////////////////////////////////
THE ERROR RETURNED:
///////////////////////////////////


jrunx.compiler.DefaultCFE: Compiler errors:
Found 1 semantic error compiling &quot;C:/JRun4/servers/scwaLab/default-ear/default-war/WEB-INF/jsp/jrun__test2ejsp9.java&quot;:

63. String sal = request.getParameter(&quot;username&quot;);
<----->
*** Error: &quot;request&quot; is either a misplaced package name or a non-existent entity.

at jrunx.compiler.JavaCompiler.compile(JavaCompiler.java:138)
at jrunx.compiler.JavaCompiler.compile(JavaCompiler.java:97)
at jrun.jsp.Translator.compilePage(Translator.java:176)
at jrun.jsp.Translator.translate(Translator.java:254)
at jrun.jsp.Translator.translate(Translator.java:101)
at jrun.jsp.JSPEngine.translateJSP(JSPEngine.java:693)
at jrun.jsp.JSPServlet.translate(JSPServlet.java:125)
at jrun.jsp.JSPServlet.service(JSPServlet.java:113)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:252)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:527)
at jrun.servlet.http.WebService.invokeRunnable(WebService.java:168)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:451)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

javax.servlet.ServletException: Translator.CompilationFailedExceptionCompiler errors:
Found 1 semantic error compiling &quot;C:/JRun4/servers/scwaLab/default-ear/default-war/WEB-INF/jsp/jrun__test2ejsp9.java&quot;:

63. String sal = request.getParameter(&quot;username&quot;);
<----->
*** Error: &quot;request&quot; is either a misplaced package name or a non-existent entity.

at jrun.jsp.Translator.compilePage(Translator.java:187)
at jrun.jsp.Translator.translate(Translator.java:254)
at jrun.jsp.Translator.translate(Translator.java:101)
at jrun.jsp.JSPEngine.translateJSP(JSPEngine.java:693)
at jrun.jsp.JSPServlet.translate(JSPServlet.java:125)
at jrun.jsp.JSPServlet.service(JSPServlet.java:113)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:252)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:527)
at jrun.servlet.http.WebService.invokeRunnable(WebService.java:168)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:451)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)


///////////////////////////////////
WORKING CODE
///////////////////////////////////

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<%@ page language=&quot;java&quot; %>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<% String sal = request.getParameter(&quot;username&quot;); %>
<%= sal%><br/>
</body>
</html>
 
I think that <%! String xx= something %> is converted to a servlet class &quot;field&quot;, therefore the methord call you're referencing is now available yet. Nor is is advisable to program any java class in such a way...
The exclamation mark is the key! do not use it in such situations....
for exmaple here is what you're trying to do:

class foo extends HttpServlet {

//here is where the it refuses to compile
string sal = request.getParameter(&quot;userName&quot;);

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

//your declation should be here...
}

Hope that helps....and someone shall correct me if i'm wrong...
 
Good advice libaax.

Essentially what happened is that the declarations appear before the methods are actually available. In your JSP page it may be better to do the following:

<%! //declerations
int x = 0;....
%>

<% //initialisations <-- different from declarations since they don't appear before!

String x = request.getParameter(&quot;x&quot;);
%>

Had a hard time figuring that one out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top