473,287 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Need Help on JSP, Bean and Servlet

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
9 3125
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
So you don't need that new Test that you create in line 15 with Test tes=new Test();
Nov 19 '08 #6
r035198x
13,262 8TB
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
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
2,364 Expert 2GB
Neat, thanks for posting your findings here, perhaps others can make use of them:-)

Later!

Dököll
Dec 6 '08 #9
r035198x
13,262 8TB
@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

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

Similar topics

3
by: Jose Munoz | last post by:
Hi all, I want to share some data for all my applications (servlets and jsps). For this i am using a JSP to set the variables with scope=application. When i get this data from some JSP all is o.k,...
0
by: Borek | last post by:
In my project I have to access in one business method of session bean usually more then 10 CMP Beans. I would like to have some utils classes, which could get me right instance of CMP or create...
2
by: Doug Lowe | last post by:
Maybe someone can help me with this--I'm pulling my hair out over it and don't have much to spare :) I'm trying to learn javabeans & jsp, I'm just getting started with it, and all I want to do...
1
by: Claire007 | last post by:
Hi... I have written the java bean below to use with my login servlet but I can't seem to find out where to retrieve my hash table results.. import java.lang.String; import...
3
by: nileshpatil1884 | last post by:
I am using apachi tomcat server. I want to set the properties of beans using servlet. But it not works ,Is any setting/permission needs? If yes than what setting are necessary & how it will do? thank...
2
by: nvidia1 | last post by:
Hi this is my first post but so far this is pretty good site. I have a file called index.html with the following code: <!-- index.html --> <html> <head><title>Student Registration...
0
by: jaisi | last post by:
What iam exactly trying to do is..saving the resultset in a bean.....and calling the bean in a servlet and forwarding it to jsp by setting the bean in the request scope; Now i have only one record...
1
by: hostel | last post by:
HTML PAGE <html> <body> <form action="second.jsp" method="get"> YOur name <input type=text name="name"> <input type=submit > </form> </body> </html>
2
by: vijaykumardahiya | last post by:
Hello Sir, I have a simple Issue but It is not resolve by me i.e input parameter are not store in Ms-Access. I store the input parameter through Standard Action <jsp:useBean>. jsp:useBean call a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.