Saturday

How to create servlets in Eclipse

   How to write & execute servlets code in eclipse ,Netbeans IDE. Don't confuse with web.xml file, eclipse can automatically generate it.Develop java servlets with tomcat.

Now, Let us start with simple Hello world program in servlets .

1. File-> New ->  Dynamic web project
Name the project as Helloworld.

2. Click next, you will get a window as follow  there check in the option Generate web.xml.




3. From the project hierarchy (at right side of window), right click Helloworld project and create new servlet file, name it as "hello.java".

  4.   Download   javax.servlet-api-3.0.1.jar 

  5. Now right click the Helloworld project -> Build path -> Libraries tab -> Add external jar -> Browse the javax.servlet-api-3.0.1.jar

  6. Inside hello.java delete the default contents  and paste the below code.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")

public class hello extends HttpServlet
{

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException


    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

   out.println("This is my 1st servlet program");


}}

  Now you successfully executed the servlets program 


Hope you already configured webserver (I ve tomcat7) which is needed to execute Dynamic web projects ..if not go here .



Servlets connection with Mysql
Servlets with doPost (get form values)




0 comments:

Post a Comment

Don't Forget To Join US Our Community
×
blogger