aspose file tools*
The moose likes Servlets and the fly likes Servlet handling multiple requests from applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet handling multiple requests from applet " Watch "Servlet handling multiple requests from applet " New topic
Author

Servlet handling multiple requests from applet

fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
Hello!
I'm trying to write a simple web app based upon applet-servlet communication!
I have a couple of servlets the first of which lets the user to log into the database

The login part of the applet is the following
//on a button click
username = userNameFld.getText().trim();
password = passFld.getText().trim();

try{
loginParameters = "?username=" + username + "&" + "password=" + password;
loginURL = new URL(protocol, hostName, port, "/servlet/AuthenticationServlet/" + loginParameters);
connection = loginURL.openConnection();
connection.setDoInput(true);
connection.setUseCaches(false);

ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
Request request = (Request) ois.readObject();
ois.close();
String access = request.getAuthorization();

if(!access.equals("success")){

userNameFld.setText(null);
passFld.setText(null);
messageLabel.setText(access);
connection = null;

}else{
//allow further actions
}

}catch(Exception e){
userNameFld.setText(null);
passFld.setText(null);
messageLabel.setText(e.getMessage());
connection = null;
}

The servlet part is the following
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection connection = null;
String username = null, password = null, access = null;

username = request.getParameter("username");
password = request.getParameter("password");

Request request = new Request();
try{
//Here go to the database and fill the request object
ObjectOutputStream ous = new ObjectOutputStream();
ous.writeObject(request);
ous.flush();
}catch(Exception e){
}

Each user has a different account so I cant use connection pooling here (although it can be created dinamically but
I dont see it suitable in my case)!
In my case the connection to the database should be alive only for the duration of the doGet() method!
I'm new to servlets and I dont know how to make so that, for example, the doGet() method of the above servlet
can handle multiple requests comming at the same time.
Any suggestion would be of much help!
Please help!

Thank you very much, Feadi!
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Each user has a different account so I cant use connection pooling here (although it can be created dinamically but I dont see it suitable in my case)!


That's not the criteria to choose connection pooling. You would use connection pooling when you dont want the overhead of creating a new connection per request. And its most suited to server programs - servlets, ejbs etc


In my case the connection to the database should be alive only for the duration of the doGet() method!


That's how it normally ought to be and it doesnt happen implicitly. You have to code for it.



You may consider doing that in a facade for cleanliness

I'm new to servlets and I dont know how to make so that, for example, the doGet() method of the above servlet can handle multiple requests comming at the same time.


That's how servlets work - a thread is spawned for each request and invokes the doGet() method - it doesnt matter if the request is from an applet/browser.

ram.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
Having a login servlet doesn't do much good if you need to open a Connection for each new servlet request - you'd need to log in again for each request. Or are you planning to keep the DB connection around in a session for that user somehow? Otherwise, remember to properly close the Connection at the end of the servlet request.

Sending a DB username and password over HTTP makes me uneasy, but in an intranet setting or over a VPN that may be OK.


Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

fedai gandjaliyev,
Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
anand bajaj
Greenhorn

Joined: Mar 20, 2006
Posts: 17
A small observation,i guess it would solve your problem



in the above code fragment.. the ObjectOutputStream is to be linked with response(HttpServletResponse) outputstream so that it can reach to the recipient.
So,try getting outputstream from response object and link it with ObjectOutputStream by passing it as a constructor argument.

Will this solve your problem???



SCJP 1.4<br />SCWCD 1.4 <br />SCDJW (coming soon)
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
I apoloize for not being aware of including code into tags!
In my case I dont keep connecions open!
With the aid of the servlet I just need to verify if the user
trying to gain access is a valid one!


Will it do what I'm trying make it to?
Will it make the servlet reply properly for requests coming
at the same time?

Thanks a lot!
fedai gandjaliyev
Ranch Hand

Joined: Dec 05, 2005
Posts: 99
I'm still waiting for help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet handling multiple requests from applet