473,624 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help on JSP, Bean and Servlet

38 New Member
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 3164
r035198x
13,262 MVP
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("So me text") as the first line of your JSP, does that text get displayed?
Nov 19 '08 #2
mjahabarsadiq
38 New Member
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 LoadServletAtSt artup.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 LoadServletAtSt artup 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 MVP
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
mjahabarsadiq
38 New Member
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 MVP
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 MVP
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
mjahabarsadiq
38 New Member
Hi

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

LoadServletAtSt artup.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 Recognized Expert Top Contributor
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 MVP
@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
6529
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, i can see the data that i saved, but when i get access with my servlets, i can't see data saved prev. My request is simple: save data with a JSP and read this data with servlets.? please if you send me a small sample to do this. or tell me...
0
1828
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 one. Instead of making the same thing again and again. My proposition looks like: 1) Class for taking home interface: public final class JNDILookup { private static Context initialContext; private static HashMap homeInterfaces; private static...
2
2407
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 is create a simple bean that will display some text on a JSP. Here's the bean: C:\Tomcat\webappsROOT\WEB-INF\classes\movie
1
1349
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 java.util.Hashtable; public class Log{
3
2245
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 you.
2
2128
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 Front</title></head> <body bgcolor="#ffffcc"> <h2 align="center"><font color="#800000">Student Registration Webapp 2007</font></h2>
0
1341
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 in the db; But still iam getting the servlet as wellas jsp with no 0 name null; Here is the code: import java.io.Serializable;
1
2665
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
3321
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 property IssueData. this property exist in SimpleBean which create a connection from DB and insert the data. At run time servlet and server also show that loggging are saved in DB. But when I open the table in Access. Its empty. Ms-Access have...
0
8238
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8174
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.