• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Servlet handling multiple requests from applet

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???

 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still waiting for help!
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic