By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,916 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Need Help on JSP, Bean and Servlet

P: 38
Hi

I have created a servlet that is to be started at the server startup. And I got it.

In that I have created a object of another class and set it as a session attribute.

What I am trying is to get the object in a jsp page.

Following are the codes.

This is the servlet:
Expand|Select|Wrap|Line Numbers
  1. package com.itpeps.test;
  2.  
  3. import java.lang.*;
  4. import java.io.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. public class LoadServletAtStartup extends HttpServlet {
  9.  
  10.   public void init() {
  11.     System.out.println("\n\n\n**************SADIQ*****************");
  12.     System.out.println(getServletName() + ": initialised" );
  13.     System.out.println("**************SADIQ*****************\n\n\n");
  14.   }
  15.  
  16. protected void service(
  17.     HttpServletRequest request,
  18.     HttpServletResponse response)
  19.   throws ServletException, IOException {
  20.       HttpSession session=request.getSession();
  21.       Test t=new Test();
  22.       t.setString("String is set from Servlet");
  23.       session.setAttribute("beanObject",t);
  24.     }
  25.  
  26.  
  27. }
web.xml:
Expand|Select|Wrap|Line Numbers
  1.   <servlet>
  2.        <servlet-name>earlyriser</servlet-name>
  3.        <servlet-class>com.itpeps.test.LoadServletAtStartup</servlet-class>
  4.        <load-on-startup>1</load-on-startup>
  5.   </servlet>
Test.java
Expand|Select|Wrap|Line Numbers
  1. package com.itpeps.test;
  2.  
  3. import java.io.*;
  4. import java.lang.*;
  5.  
  6. public class Test
  7. {
  8.     String temp;
  9.  
  10.     public void setString(String t)
  11.     {
  12.         temp=t;
  13.     }
  14.     public String getString()
  15.     {
  16.         return temp;
  17.     }
  18. }
index.jsp
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Online Reservation System</title>
  4. </head>
  5. <body LEFTMARGIN=0 RIGHTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
  6.     <font color="#blue">
  7.     <%@page import="java.io.*"
  8.         import="java.lang.*"
  9.         import="java.sql.*"
  10.         import="javax.naming.*"
  11.         import="javax.sql.*"
  12.         import="com.itpeps.test.*"
  13.     %>
  14.         <%    
  15.             Test tes=new Test();
  16.             tes=(Test)session.getAttribute("beanObject");
  17.             String selDB = tes.getString();
  18.         %>
  19.     <%=selDB%>
  20. </body>
  21. </html>
The servlet startup is working fine.

While accessing the index.jsp i got the following error in the log.

Expand|Select|Wrap|Line Numbers
  1. 2008-11-19 00:35:35 Authenticator[/onlineres]: Security checking request GET /onlineres/index.jsp
  2. 2008-11-19 00:35:35 Authenticator[/onlineres]:  Not subject to any constraint
  3. 2008-11-19 00:35:35 StandardContext[/onlineres]: Mapping contextPath='/onlineres' with requestURI='/onlineres/index.jsp' and relativeURI='/index.jsp'
  4. 2008-11-19 00:35:35 StandardContext[/onlineres]:  Mapped to servlet 'jsp' with servlet path '/index.jsp' and path info 'null' and update=true
  5. 2008-11-19 00:35:35 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
  6. org.apache.jasper.JasperException
  7.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
  8.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
  9.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
  10.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
  11.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
  12.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
  13.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
  14.     at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
  15.     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
Please help me to sort out this problem. My mail id is <removed>

Thanks in advance.
M. Jahabar Sadiq
Nov 18 '08 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
1.) It is good practice to place page directives at the top of the jsp page.
2.) You can use one import and specify the packages as a comma separated list rather than putting many import statements.
3.) If you put an out.println("Some text") as the first line of your JSP, does that text get displayed?
Nov 19 '08 #2

P: 38
Hi,

Thanks for your valuable comments. I will correct them.

Actually what I would like to do is, to create a start up servlet and it should create an object of a class. I have to access the created object from a jsp page. Here I am giving the folder structure.

My application folder name is "onlineres". I am deploying the application using ant and the application is getting deployed in the following folder of the tomcat.

TOMCATE_HOME/work/standalone/localhost/onlineres

Following is the folder structure within the folder "onlineres"

/onlineres/src/com/itpeps/test
In this the Test.java and LoadServletAtStartup.java are present.

/onlineres/WEB-INF/classes/com/itpeps/test
In this the corresponding classes for the above said java programs are available

/onlineres/WEB-INF
In this the web.xml file is available

/onlineres/web
In this the index.jsp file is available


I am running the tomcat from the port 8888.

While starting the server, the LoadServletAtStartup servlet is getting initiated successfully.

But while accessing the index.jsp file using the URL http://localhost:8888/onlineres/index.jsp I got the errors that I have mentioned in my starting post.

Before including these servlet related changes in the jsp, I can able to create object for the class Test in the jsp and the page was getting displayed correctly.

Please tell me, what is wrong here. Whether can I achieve what I would like to get? If this the wrong way, kindly tell me any other ways.

thanks in advance,
M. Jahabar Sadiq.
Nov 19 '08 #3

10K+
P: 13,264
But your JSP is not getting the object that was created in the servlet at all. It's creating a new Test object which will be different from the one created in the servlet. Better put a getter for that object in the servlet and call that getter from the JSP.
Nov 19 '08 #4

P: 38
In my servlet, I put the object in the session's attribute "beanObject" using setAttribute method. Also I am getting the object in JSP from the session using getAttribute method.

Whether the Servlet and the JSP are in different sessions? If so how could I make avail the servlet's to all jsp page.
Nov 19 '08 #5

10K+
P: 13,264
So you don't need that new Test that you create in line 15 with Test tes=new Test();
Nov 19 '08 #6

10K+
P: 13,264
Another thing, since that servlet is loaded by the server on startup and no requests are made to it, it's service method is never called. That means that attribute is never put in the session. If the object needs to be created when the server starts up then create a getter for it like I suggested above.
Nov 19 '08 #7

P: 38
Hi

I got the result what i would like to have by changing the code as follows.

LoadServletAtStartup.java
--------------------------------------
Changed the code in the line no: 23

Previously it was
Expand|Select|Wrap|Line Numbers
  1.        session.setAttribute("beanObject",t);
I have changed it as
Expand|Select|Wrap|Line Numbers
  1.        getServletContext().setAttribute("beanObject",t);

Index.jsp:
--------------
Changed the code in the line no: 15 and 16

Previously it was
Expand|Select|Wrap|Line Numbers
  1.             Test tes=new Test();
  2.             tes=(Test)session.getAttribute("beanObject");
I have changed it as
Expand|Select|Wrap|Line Numbers
  1.              Test tes=(Test)getServletContext().getAttribute("beanObject");
Now I got the result as expected. Now I have another doubt. I will create a new Thread for this.

Thanks for your valuable comments.
Dec 6 '08 #8

Dököll
Expert 100+
P: 2,364
Neat, thanks for posting your findings here, perhaps others can make use of them:-)

Later!

Dököll
Dec 6 '08 #9

10K+
P: 13,264
@mjahabarsadiq
Don't forget to make that thread a daemon unless if you want it to keep running after the server is stopped.
Dec 10 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.