Need Help on JSP, Bean and Servlet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjahabarsadiq
    New Member
    • Feb 2007
    • 38

    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:
    Code:
    package com.itpeps.test;
    
    import java.lang.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class LoadServletAtStartup extends HttpServlet {
    
      public void init() {
    	System.out.println("\n\n\n**************SADIQ*****************");
        System.out.println(getServletName() + ": initialised" );
    	System.out.println("**************SADIQ*****************\n\n\n");
      }
    
    protected void service(
        HttpServletRequest request,
        HttpServletResponse response)
      throws ServletException, IOException {
    	  HttpSession session=request.getSession();
    	  Test t=new Test();
    	  t.setString("String is set from Servlet");
    	  session.setAttribute("beanObject",t);
        }
    
    
    }
    web.xml:
    Code:
      <servlet>
           <servlet-name>earlyriser</servlet-name>
           <servlet-class>com.itpeps.test.LoadServletAtStartup</servlet-class>
           <load-on-startup>1</load-on-startup>
      </servlet>
    Test.java
    Code:
    package com.itpeps.test;
    
    import java.io.*;
    import java.lang.*;
    
    public class Test
    {
    	String temp;
    
    	public void setString(String t)
    	{
    		temp=t;
    	}
    	public String getString()
    	{
    		return temp;
    	}
    }
    index.jsp
    Code:
    <html>
    <head>
    	<title>Online Reservation System</title>
    </head>
    <body LEFTMARGIN=0 RIGHTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
    	<font color="#blue">
    	<%@page import="java.io.*"
    		import="java.lang.*"
    		import="java.sql.*"
    		import="javax.naming.*"
    		import="javax.sql.*"
    		import="com.itpeps.test.*"
    	%>
    		<%	
    			Test tes=new Test();
    			tes=(Test)session.getAttribute("beanObject");
    			String selDB = tes.getString();
    		%>
    	<%=selDB%>
    </body>
    </html>
    The servlet startup is working fine.

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

    Code:
    2008-11-19 00:35:35 Authenticator[/onlineres]: Security checking request GET /onlineres/index.jsp
    2008-11-19 00:35:35 Authenticator[/onlineres]:  Not subject to any constraint
    2008-11-19 00:35:35 StandardContext[/onlineres]: Mapping contextPath='/onlineres' with requestURI='/onlineres/index.jsp' and relativeURI='/index.jsp'
    2008-11-19 00:35:35 StandardContext[/onlineres]:  Mapped to servlet 'jsp' with servlet path '/index.jsp' and path info 'null' and update=true
    2008-11-19 00:35:35 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
    org.apache.jasper.JasperException
    	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
    	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
    	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    	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
    Last edited by Nepomuk; Nov 18 '08, 07:40 PM. Reason: Please read the Posting Guidelines - you must not put email addresses in your posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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?

    Comment

    • mjahabarsadiq
      New Member
      • Feb 2007
      • 38

      #3
      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.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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.

        Comment

        • mjahabarsadiq
          New Member
          • Feb 2007
          • 38

          #5
          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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            So you don't need that new Test that you create in line 15 with Test tes=new Test();

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              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.

              Comment

              • mjahabarsadiq
                New Member
                • Feb 2007
                • 38

                #8
                Got the result

                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
                Code:
                       session.setAttribute("beanObject",t);
                I have changed it as
                Code:
                       getServletContext().setAttribute("beanObject",t);

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

                Previously it was
                Code:
                            Test tes=new Test();
                            tes=(Test)session.getAttribute("beanObject");
                I have changed it as
                Code:
                             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.
                Last edited by Nepomuk; Dec 10 '08, 02:42 PM. Reason: Please use [CODE] tags

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  #9
                  Neat, thanks for posting your findings here, perhaps others can make use of them:-)

                  Later!

                  Dököll

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by mjahabarsadiq
                    ..
                    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.
                    Don't forget to make that thread a daemon unless if you want it to keep running after the server is stopped.

                    Comment

                    Working...