Usage of NotesThread in the servlet's init() method

Hi All,

i am not able to get the session object which is created in the servlet’s init mehtod when i use the session object in other methods.

when i say prinln(session), it is returning null

but the session object is not null.i have checked the same using

println(“session >>” +session);

returns: session >> null

when i check like below, it is not going to else conditon and going to do…

if(session!= null)

do…

else

println(“session null”)

wondering whether i am using the NotesThread at the right place i.e in the init method.

public void init(ServletConfig config) throws ServletException {

	super.init(config);

	try {

			NotesThread.sinitThread();

			session_ = NotesFactory.createSession();

			System.out.println("Session Established >>"+session_);

		} 

	catch (NotesException ne) {

			System.out.println(ne.getMessage());

			ne.printStackTrace();

		}



}

Subject: Usage of NotesThread in the servlet’s init() method.

I’m not a Java programmer but aren’t u supposed to output a property instead of just session. I would try outputting ex. System.out.println("Common user name = " + session.getCommonUserName())

Might be wrong :wink:

Subject: RE: Usage of NotesThread in the servlet’s init() method.

I am getting the session object in the init() method but when i use the same session object in other methods i am getting exception

Thanks

CODE


/**

*	This method is called by the server immediately after the server 

*	constructs the servlet's instance

*	Used to Establish the Notes Session which will be used after words

*	@param arg A config of type javax.servlet.ServletConfig 

*/

public void init(ServletConfig config) throws ServletException {

	super.init(config);

	try {

			NotesThread.sinitThread();

			session_ = NotesFactory.createSession();

			System.out.println("Session Established >>"+session_);// I am getting the session

		} 

	catch (NotesException ne) {

			System.out.println(ne.getMessage());

			ne.printStackTrace();

		}



}



/**
  • Connect to the notes db…

  • @param arg A database of type String .

  • @return No return value.

  • @exception NotesException If the Database is not found or other error.

*/

public void connectDB(String database) {

	try {

			System.out.println("Session Established 000 >> "+session_);// returning null as Session Established 000 >> null



			db_ = session_.getDatabase(null,database);



			System.out.println("Check"+db_.getTitle());



		}  catch (NotesException ne) {

			System.out.println(ne.getMessage());

			ne.printStackTrace();

		}

}

public void service(HttpServletRequest request, HttpServletResponse response) {

	try {

		System.out.println("Servlet is called");



		response.setContentType("text/html");

		PrintWriter pWriterToBrowser_ = response.getWriter();

		Collection documents_;

		int start_;

		String serverName_ = (String)request.getParameter("server");

		String databaseName_= (String)request.getParameter("database");

		String viewName_ = (String)request.getParameter("view");

		System.out.println("Server "+serverName_ + "\t" + "Database" + databaseName_ + "\t" + "View "+viewName_);

		System.out.println("Begin "+request.getParameter("start") );

		System.out.println("Query String  " + request.getQueryString());

		System.out.println("Requested URI  " + request.getRequestURI());

		// Connecting the DB

		if (serverName_ != null) {

			connectDB(serverName_, databaseName_) ;

		}

		else {

			connectDB(databaseName_) ;

		}

               }

	catch (Exception ne) {

		System.out.println(ne.getMessage());

		ne.printStackTrace();

	}

EXCEPTION:

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP Server: Using Web Configuration View

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM JVM: Java Virtual Machine initialized.

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP Server: Java Virtual Machine loaded

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM Servlet engine initialization was successful

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP JVM: com.javaworld.servlet.domino.ExampleServlet: init

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP JVM: com.javaworld.servlet.domino.DominoSubmitServlet: init

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP JVM: com.javaworld.servlet.domino.DominoServ: init

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP JVM: com.javaworld.servlet.domino.DominoViewServlet: init

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP JVM: Session Established >>CN=MERCURY/O=HCLTESTsession_.toString()CN=MERCURY/O=HCLTEST

[0A2C:0002-0BFC] 04/27/2004 04:36:41 PM HTTP JVM: com.javaworld.servlet.domino.names: init

[0A2C:0002-0BFC] 04/27/2004 04:36:42 PM HTTP Server: Started

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Servlet is called

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Server null Databasenull View null

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Begin null

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Query String

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Requested URI /servlet/domViewServlet

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: null

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: NotesException: A required argument has not been provided

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.local.Database.NgetView(Native Method)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.local.Database.getView(Unknown Source)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.getView(DominoViewServlet.jav

a:135)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.service(DominoViewServlet.jav

a:551)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.executeServlet(DominoServletInvoke

r.java:283)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.service(DominoServletInvoker.java:

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.servlet.ServletManager.service(ServletManager.java:245)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: null

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: java.lang.NullPointerException

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.writeHTMLTable(DominoViewServ

let.java:198)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.service(DominoViewServlet.jav

a:581)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.executeServlet(DominoServletInvoke

r.java:283)

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.service(DominoServletInvoker.java:

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: at lotus.domino.servlet.ServletManager.service(ServletManager.java:245)

Subject: Its not the session that is null…

Its your parameters, here is your output:[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Server null Databasenull View null

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Begin null

[0A2C:000A-0C84] 04/27/2004 04:36:56 PM HTTP JVM: Query String

Here is your code:

		String serverName_ = (String)request.getParameter("server");

		String databaseName_= (String)request.getParameter("database");

		String viewName_ = (String)request.getParameter("view");

		System.out.println("Server "+serverName_ + "\t" + "Database" + databaseName_ + "\t" + "View "+viewName_);

		System.out.println("Begin "+request.getParameter("start") );

		System.out.println("Query String  " + request.getQueryString());

Looks to me like none of your parameters are in request. No server, database, view or QueryString

Subject: RE: Its not the session that is null…

Yes, you are right. I pasted the wrong console info. Below is the right one with all the paramsThanks for your time.


l http

[06C8:0002-0D74] 04/27/2004 11:10:42 PM HTTP Server: Using Web Configuration View

[06C8:0002-0D74] 04/27/2004 11:10:42 PM JVM: Java Virtual Machine initialized.

[06C8:0002-0D74] 04/27/2004 11:10:42 PM HTTP Server: Java Virtual Machine loaded

[06C8:0002-0D74] 04/27/2004 11:10:42 PM Servlet engine initialization was successful

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP JVM: com.javaworld.servlet.domino.ExampleServlet: init

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP JVM: com.javaworld.servlet.domino.DominoSubmitServlet: init

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP JVM: com.javaworld.servlet.domino.DominoServ: init

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP JVM: com.javaworld.servlet.domino.DominoViewServlet: init

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP JVM: Session Established >>CN=MERCURY/O=HCLTEST

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP JVM: com.javaworld.servlet.domino.names: init

[06C8:0002-0D74] 04/27/2004 11:10:43 PM HTTP Server: Started

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: Servlet is called

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: Server null DatabaseCandleSR.nsf View vw.OpenBySeverity

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: Begin null

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: Query String database=CandleSR.nsf&view=vw.OpenBySeverity

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: Requested URI /servlet/domViewServlet

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: Session Established 000 >> null database:CandleSR.nsf

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: null

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: NotesException: Cannot service method call on uninitialized thread

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.local.Session.NgetDatabase(Native Method)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.local.Session.getDatabase(Session.java:601)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.connectDB(DominoViewServlet.j

ava:93)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.service(DominoViewServlet.jav

a:540)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.executeServlet(DominoServletInvoke

r.java:283)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.service(DominoServletInvoker.java:

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.servlet.ServletManager.service(ServletManager.java:245)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: null

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: java.lang.NullPointerException

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.getView(DominoViewServlet.jav

a:128)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at com.javaworld.servlet.domino.DominoViewServlet.service(DominoViewServlet.jav

a:545)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at javax.servlet.http.HttpServlet.service(HttpServlet.java:588)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.executeServlet(DominoServletInvoke

r.java:283)

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.servlet.DominoServletInvoker.service(DominoServletInvoker.java:

[06C8:000B-085C] 04/27/2004 11:10:57 PM HTTP JVM: at lotus.domino.servlet.ServletManager.service(ServletManager.java:245)

Subject: Ok, where is the session_ variable declared, also NotesThread?

Perhaps they have been garbage collected?

Subject: RE: Ok, where is the session_ variable declared, also NotesThread?

Below is the complete code of the servlet.session_ variable is declared as class variable private and NotesThread is initialized in servlets init() method.

How could be the session object got garbage collected ? It is avaiable in the servlets init() method and not avaiable same after words?. is there any thing i need to take care about the NotesThread or some thing related to Domino Servlet Engine?.


package com.javaworld.servlet.domino;

// Java Classes

import java.io.PrintWriter;

import java.io.IOException;

import java.util.*;

//Servlet Classes

import javax.servlet.*;

import javax.servlet.http.*;

//Notes Classes

import lotus.domino.*;

public class DominoViewServlet extends HttpServlet {

//Notes 

private Session session_;

private Database db_;

private View vw_;

private Document doc_;



private int numberOfDocuments_;



/**

*	This method is called by the server immediately after the server 

*	constructs the servlet's instance

*	Used to Establish the Notes Session which will be used after words

*	@param arg A config of type javax.servlet.ServletConfig 

*/

public void init(ServletConfig config) throws ServletException {

	super.init(config);

	try {

			NotesThread.sinitThread();

			session_ = NotesFactory.createSession();

			System.out.println("Session Established >>"+session_);

		} 

	catch (NotesException ne) {

			System.out.println(ne.getMessage());

			ne.printStackTrace();

		}



}



/**
  • Connect to the notes db…

  • @param arg A database of type String .

  • @return No return value.

  • @exception NotesException If the Database is not found or other error.

*/

public void connectDB(String database) {

	try {

			System.out.println("Session Established 000 >> "+session_.toString()+" database:"+database);

			db_ = session_.getDatabase(null,database);



			System.out.println("Check"+db_.getTitle());



		}  catch (NotesException ne) {

			System.out.println(ne.getMessage());

			ne.printStackTrace();

		}

}

public void service(HttpServletRequest request, HttpServletResponse response) {

	try {

		System.out.println("Servlet is called");



		response.setContentType("text/html");

		PrintWriter pWriterToBrowser_ = response.getWriter();

		Collection documents_;

		int start_;

		String serverName_ = (String)request.getParameter("server");

		String databaseName_= (String)request.getParameter("database");

		String viewName_ = (String)request.getParameter("view");

		System.out.println("Server "+serverName_ + "\t" + "Database" + databaseName_ + "\t" + "View "+viewName_);

		System.out.println("Begin "+request.getParameter("start") );

		System.out.println("Query String  " + request.getQueryString());

		System.out.println("Requested URI  " + request.getRequestURI());

		// Connecting the DB

		if (serverName_ != null) {

			connectDB(serverName_, databaseName_) ;

		}

		else {

			connectDB(databaseName_) ;

		}

                }

catch (Exception ne) {

		System.out.println(ne.getMessage());

		ne.printStackTrace();

	}

}

}