• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in Using Servlet Context

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
why don't you store those values in a database and let the other servlet acess the database.
regards
andrew
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Rgds Kunal
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
santosh akella
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, just post the method signature for your doPost and doGet methods.
[ December 31, 2004: Message edited by: Ben Souther ]
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



The pathname must begin with a "/" and is interpreted as relative to the current context root.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What servlet container are you using?
 
santosh akella
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
What servlet container are you using?

 
santosh akella
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic