aspose file tools*
The moose likes Servlets and the fly likes Problem in Using Servlet Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in Using Servlet Context" Watch "Problem in Using Servlet Context" New topic
Author

Problem in Using Servlet Context

santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
hi

i am creating an email system Project. for which i nam taking an username and Password from one servlet. and i want to pass this username to another .
so, i started using ServletContext class and getServletContext () Method .
but i am getting error because its taking null value.
can , anyone pls help me
thank u
arun andrew
Greenhorn

Joined: Dec 21, 2004
Posts: 7
hi,
why don't you store those values in a database and let the other servlet acess the database.
regards
andrew
Kunaal A Trehan
Ranch Hand

Joined: Feb 04, 2004
Posts: 63
Hi,

One approach cud be getting the values and setting in session.
So that u can pick the attribute whenever reqd.

Rgds Kunal
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How are you moving from one servlet to another?
With a requestDispatcher (chaining), sendRedirects, or are you relying on the user to click links and submit forms?

If you're chaing servlets, the most efficient thing to do is to bind your variables (objects) to the request object and have the next servlet retrieve them from there.

If you're using sendRedirect, you can add them to the querystring.

If you're relying on the user, you can either add them to the querystring or use hidden variables.

And yes, you can always bind object to the session but that's probably overkill unless you have a specific reason to keep them around for the whole session.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
basically i depending on user to click the button.

what i have done is , i have created a servlet page for login. which in turn
shows the Inbox button after ur logged in . this Inbox calls another Servlet
which shows the EMails. but i am not able to get login username to inbox servlet . so, it doesnt depend on user login for showing different inboxs for differnt logins .
and i am new in using ServletContext class.
so, i have doubts regarding using it.
like should we use HttpSession for creating a session.
after creating this session when i say HtttpSession.getAttribute("String");
it is showing method not Found. so, pls tell me how to use it exactly

thank u
dhani dhani
Greenhorn

Joined: Nov 04, 2004
Posts: 6
If I understand your requirements, Once user is logged in, For any subsequent clicks, you need to remember the user id and password?.
If this is your requirement, I strongly suggest you to store userid and password in session or to make it more secure and object oriented, create class, which has
String userId
char[] password [Note password is char array because Strings are immutable. There is security risk involved in storing passowrd in String.]

Store this class in HttpSession object. This is more efficent than storing user id and password as two separate object in session.

How are you calling request.getSession().
Please pass in the false, So it uses already created session or you will get null session, so you can create new session.
I hope this helps you...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

after creating this session when i say HtttpSession.getAttribute("String");
it is showing method not Found. so, pls tell me how to use it exactly



Assuming that you've named your request "request" in the doPost signature..


Then, to retrieve it...

santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
the problem is that whenever i use request.getRequestDispatcher("some string");
or request.getAttribute(""); or any method i call by request i am getting an error like method not found .
i am using JSDK2.0 is that something to do with the errors or coding is error

thanku
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is no getRequestDispatcher method in the request object.
getRequestDispatcher is a method of the servletContext object.

getServletContext().getRequestDispatcher("/index.jsp");



Post your servelet code. Make sure to include the method signature for your doPost or doGet (whichever you are using) method.
[ December 31, 2004: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In fact, just post the method signature for your doPost and doGet methods.
[ December 31, 2004: Message edited by: Ben Souther ]
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
Do you mean that getServletContext actually returns null ? something like:



If so, could it be an error in your "init" method ? I saw it happen sometimes with servelts that have an "init" method that neglects to call super.init() :
santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
Thnak u very much Sol Mam-Orn .
but i still have some problem with RequestDispatcher
my code goes like this
public class user extends HttpServlet {
Connection con;
PreparedStatement pst;
public static String user1;
public void init(ServletConfig conf){

try{
super.init(conf);
................
....
}
catch(Exception e)
{
System.out.println(e);
}}
public void service(HttpServletRequest req,HttpServletResponse res )

throws ServletException, IOException
{
.........
....
//and at last
ServletContext context=this.getServletContext();
RequestDispatcher rd=context.getRequestDispatcher("sendmailserv1");
rd.forward(req,res);
}
}

it is saying
cannot resolve the symbol
symbol: method getRequestDispatcher(string);


thank u once again....
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
You got me there... I even tried to copy/paste your code, and it compiled fine on my environment.

The only (far-fetched) possibilities I'd check for:
- typing errors
- could there be another class called "ServletContext" , somewhere in your classpath ? You mihgt like to change the delcaration to the fully qualified class name:


Of course, these are far fetched... but I simply can't imagine any other reason that would cause the problem. Very strange indeed.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

RequestDispatcher rd=context.getRequestDispatcher("sendmailserv1");


The pathname must begin with a "/" and is interpreted as relative to the current context root.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
i am using JSDK2.0 is that something to do with the errors or coding is error

That version is rather out of date - any particular reason for using it?
The download page for JSDK2.0 at Sun has this comment:
This page contains links to early versions of Java Servlet technology.

Bill
santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
i have loaded JSDK2.1 and tried with it but it didnt work ...
is JSDK2.1 the latest version or anything later ...
is there any other solution for it ...
pls help me out .
thanq
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What servlet container are you using?
santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
may be i have a problem in setting the path becoz if i change some semicolons
in the path , while compiling it is telling RequestDispatcher method not found , the i again changed it back to normal then it is saying ServletContext.setAttribute method not found....

i am setting the path as follows :

set path=d:\j2sdk1.4.1_02\bin;d:\jsdk2.1;d:\JSDK2.0\bin;d:\j2sdkee1.2.1\bin;
set CLASSPATH=d:\j2sdk1.4.1_02\lib\src.jar;d:\JSDK2.0\lib\jsdk.jar;d:\jsdk2.1\servlet.jar;d:\j2sdkee1.2.1\lib\j2ee.jar;

whether its right or wrong ....
pls help me
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ben Souther:
What servlet container are you using?
santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
hi ben

Actually i havent used any Servlet containers like TOMCAT or JAVA WEBSERVER,.....etc

i am just using an Emailserver for which i got source code from net and made some chages in it like portno ...etc ...

and i am trying to connect to it . thats all ..
Should i use any Servlet container
thank u
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, servlets are meant to be run in containers.

Tomcat is open source and thus free. Download and intall it.
Then, if you want a working example with a requestDispatcher,
got to: http://simple.souther.us and download SimpleMVC.

The whole process should take about a half an hour.
santosh akella
Ranch Hand

Joined: Oct 24, 2004
Posts: 32
thank u all

at last i solved my problem regarding servletcontext,.....

i have a problem in displaying servlets in a chain.. i would post this question as a new topic

thank u once again ben,sol mam -orn
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in Using Servlet Context