File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet query" Watch "Servlet query" New topic
Author

Servlet query

Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Hi,

I created a Servlet and when i started to run it in localhost, it immediately access the doGet() method and throws error I have given.

My Requirements are

1) It should not run immediately once i start the server

2) It should run only when I access it from my client. Is it possible to do that?

I have given my code below:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

How do you run it ,directly on right click ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
What are "error I have given"? I see no errors mentioned.


Ping & DNS - updated with new look and Ping home screen widget
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Niraj,

Ya. while running in localhost i right click and run.

Ulf,

I mentioned about the 404, No such users found error.

Let me give you guys a clear point on what I want.

When I run the above mentioned servlet in tomcat, it displays the following in my browser

HTTP Status 404 - No Such user exists

type Status report

message No Such user exists

description The requested resource is not available.

Apache Tomcat/7.0.34


I am not able to access the servlet after that.

Now what I want is:

I want the get method and post method to be executed when i call it from my client
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9329
    
110

Partheban Udayakumar wrote:Hi,

I created a Servlet and when i started to run it in localhost, it immediately access the doGet() method and throws error I have given.



I don't see how any servlet will run the doGet() method unless there's a request coming in. Of course the servlet's init() method can potentially be called without any requests, but not the doGet() method. Furthermore, you seem to indicate in your later reply that you are seeing that message in a browser, which effectively means that the user of that browser triggered that request.

[My Blog] [JavaRanch Journal]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
I mentioned about the 404, No such users found error.

I don't see where you had mentioned that, but that's irrelevant now that we know what happens. That message would be an expected outcome if the username/password passed to the servlet did not match an actual user, or if no parameters were passed to it. I'm confused by you saying you "right-clicked" on something to run the servlet. You would need an HTML form for entering the parameters, or you would have to add the parameters to the URL for this to work - which one of these are you trying to make work?

I want the get method and post method to be executed when i call it from my client

A "client" is a browser, or code that accesses this servlet programmatically?

(A couple of side notes: This code must not be used in a production setting as it is wide open to SQL injection attacks; use a PreparedStatement in lines 71-73 to guard against that. It also passes credentials via a GET, which is another security problem. And it uses GET and POST interchangeably, which generally violates the HTTP specification - I'd really like to find out who started this nonsense with a processRequest method that is called from both doGet and doPost; that person ought to be taken out back and shot for crimes against software engineering.)
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Jaikaran,

That's what I am worried about. Even before I run my client the page I mentioned about appears. I don't know how.

Just starting the servlet with tomcat brings that page.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9329
    
110

Partheban Udayakumar wrote:
Just starting the servlet with tomcat brings that page.


So you are saying a browser opens up on its own when you start Tomcat? Is that what you mean about "right click and run"? What are you right clicking on and where?
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Ulf,

This is just a test code. There will be implementations of security after this. Anyway thanks for the idea of security.

I am passing the credentials from the Url using setRequestProperty() method and yes my client is a java program that access the servlet.

I have one more doubt. If my client wants to access the server anytime, does the servlet need continuous execution? I mean should it be kept running continuously?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
If my client wants to access the server anytime, does the servlet need continuous execution? I mean should it be kept running continuously?

Servlets only execute when an HTTP request is sent to them. Obviously the servlet container in which they are deployed needs to run whenever you want to access that servlet.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Jaikiran,

I am using netbeans to run servlets. So to run a servlet, we just right click on the servlet code and give run. Once tomcat starts, browser pops up with the message I have given you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
That sounds like there are no parameters being passed - so what you're seeing would be expected behavior.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Ulf,

As I already mentioned, I haven't started my client yet. The error message comes once I run the servlet from Netbeans.

Has this happened to any one?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39551
    
  27
Let me put it another way: You click on something that apparently says "run servlet", and then you are surprised that the servlet runs - what else did you expect?

This is part of the reason that I advise beginners not to use an IDE for servlet development until they understand web apps and servlet containers - people tend to battle the IDE more than heir servlets.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Ulf,

Now I get the difference. So what should i do now? Should I run only the tomcat container without running the specific servlet?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9329
    
110

It looks like you are learning Servlets and trying to understand how all the pieces work together. I think it's a good time to pick one of the books on Servlets to understand this better. Like Ulf says, it's a good idea to stay away for the IDE integration for developing web applications, till you understand the basics.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 160

Ulf and Jaikiran,

Thanks for your help. I will definitely read some books and try running apps without ide.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet query
 
Similar Threads
i have a problem insert data into database but i didnt get any error
Variable is not getting transferred from controller servlet to model
Login code problem
user authentication and dao
Problem in Login using Servlets