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
- 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);
- }
- }
Expand|Select|Wrap|Line Numbers
- <servlet>
- <servlet-name>earlyriser</servlet-name>
- <servlet-class>com.itpeps.test.LoadServletAtStartup</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
Expand|Select|Wrap|Line Numbers
- 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;
- }
- }
Expand|Select|Wrap|Line Numbers
- <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>
While accessing the index.jsp i got the following error in the log.
Expand|Select|Wrap|Line Numbers
- 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)
Thanks in advance.
M. Jahabar Sadiq