0

I am new to servlets. I have a query processor java program and now, I want to use it in a Web Application. I have an interface(HTML) which generates the query and I want to run the program on a button click in the interface. For this, I want to convert the java program into a java servlet. I am working in Net Beans.

Following is the structure of my Java program :

public class ABC
{
  //code
  public ABC() //constructor
  {
   //code
  }
  public static void main(String[] args)
  {
   //code
  }
}

I want to convert this into a servlet. Following is the structure of a default servlet in Net Beans.

public class Demo extends Httpservlet
{
 /*----
  ----
  ----
  ----*/
 public void processRequest(HttpServletRequest request, Httpservlet response) 
 throws ServletException,IOException
 {
  /*code*/
 }
 /*HttpServlet methods - doGet(), doSet() etc.*/
} 

Is there any alternative for the main function in the servlet? Which method is executed first when the sevlet starts running? Can I run the Java Program on a button click on a HTML page so that I can eliminate the use of servlet?

2
  • You probably need to write your main method contents in either doGet or doPost method. Or you may invoke the main method from those do* methods directly. If you are not sure what these do* methods are, then you will have to read about servlets a bit first Commented Apr 22, 2014 at 9:42
  • There is no main method in servlet. What do you mean by invoking the main method from the do* methods? @Hirak Commented Apr 23, 2014 at 5:38

4 Answers 4

1

use get or post method in servlet depend on your action. There are doGet , doPost and so many HTTP methods you need to determine in which you write code

Sign up to request clarification or add additional context in comments.

Comments

0

To use your query processor on the web you will have to build a Java Web Application. Try the tutorial below and then call your ABC class from a Servlet.

Introduction to Developing Web Applications

Comments

0

Create a Dynamic web project, add new servlet usee doGet method or doPost method refer this link for the same.

servlet example

Hope this helps.

Comments

0

Please have in mind that the purpose of use is different in those two cases. While the main method of a class is invoked when you compile and run it as part of an application (run on a machine), the doGet and doPost methods are invoked in a servlet after a GET/POST request is made by client side to the server side, on which the Servlet lives.

On the first case, usually everything occurs on a specified machine, following the logic "do something, then done", and on the second case, you have a Request/Response Model between Clients and a Server (following the logic "do something when asked, then wait for being asked again"). You need to have a Server (e.g. Tomcat) set up to use the servlets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.