Wednesday 24 June 2015

Programming with servlets

For running the servlet program using command prompt, you will need JDK and JSDK

Download JSDK2.0 or JSDK2.1

Download JDK 1.6 or higher versions

Simple program using servlets:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Myfirstservlet extends HttpServlet
{
           public void doGet(HttpServletRequest req, HttpServerletResponse res) throws ServletException, IOException
            {
                  PrintWriter out;
                  String str="Server Side Programming";
                  res.setContentType("text/html");
                  out=res.getWriter();
                  out.println("<HTML><HEAD><TITLE>");
                  out.println(str);
                  out.println("</TITLE></HEAD><BODY>");
                  out.println("<H1>"+str+"</H1>");
                  out.close();
              }
}

Running the above servlet program through Command prompt :

Step 1: Type the above the servlet program in notepad or anyother text editors, and save as filename.java. Here filename should be same as that of your class name.
Step 2: Open the command prompt and set the path to the location where java resides in your computer. For running servlet programs, you will need jdk1.6 or higher versions .
Step 3: Set classpath as  C:\jsdk2.0\src or c:\jsdk2.0\lib\jsdk.jar; check and verify the classpath according to your version and location.
Step 4: Compile the program javac Filename.java
Step 5: Go to the location c:\jsdk2.0\bin and type servletrunner -d yourdirectory Ex: c:\jsdk2.0\bin and press enter then type servletrunner -d d:\yourname
Step 6: Open your file and see the result in browser.


No comments:

Post a Comment