Discussion Locked

Servlets

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Is it possible to call a Servlet class in HTML without using a form and doing it automatically when the page loads? Also, if a class has a main method and not a doGet() or doPost(), is it still called a Servlet or something else? Can that be called the same way as a Servlet w/ doGet() or doPost()?
Thanks in advance.

2 replies

by sparkytd

The first question, I think the answer is no. Or atleast, if you do, you'd have to call it via some sort of http post/get method - ie. invoke it via the url (www.x.com?param1=pointToClassFile) where if param1 is present, then you can load up your servlet class. But keep in mind that you would still need to check if that url param is present, and to call the servlet if it is, thus the easiest way is via jsp pages, where you'd include the servlet in your jsp. Also, remember that you still would need jakarta-tomcat, or another servlet container web server to run the servlet off of.

The second question, if the class does not have a doGet() or doPost(), then in theory, it is not a servlet. Sun doc states you must override these two methods to invoke your servlet info(if i'm not mistaken). Now, what you could do, is have a single servlet class take in the requests via doget & dopost, and send users to sub class files through your servlet. Those sub class files can be straight java files, or they can be beans, it's really up to you.

I hope this helps,

Cheers,

by boyarsky

For your first question, you should really use a JSP. Then you can have java code intertwined with your HTML. A servlet should generate the whole HTML page, not just part of it.

If you really want to call a servlet, you can do so automatically when the page loads and have the servlet display the whole HTML page.

For example, have the user type in (or link to) http://www.page.com/servlets/Dummy
Make sure you have a doGet method as it will be called. Then Dummy.java can output the wholeHTML page.

You could also redirect the user to this servlet from an HTML page rather than have them type it in directly.

Good luck.

This discussion is closed to new replies.