{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Writing Your First Servlet

Writing Your First Servlet - ChangetheClasspath > Important...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Writing Your First Servlet Change the Classpath >> Important << Before you do anything, make sure the compiler will be able to find the servlet.jar file. The servlet.jar file contains Java Web Server's implementation of the Servlet API. There are two ways to achieve this: 1. change the classpath in your system settings permanently. This option is highly recommended since it will apply in the future when you want to compile your servlet. Write the servlet Start your favorite file editor. Then type or copy the code listing below: //Import needed Java packages import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Create a class which inherits from GenericServlet or HttpServlet. public class MyFirstServlet extends HttpServlet { /** * Override the service method. * Here we handle the HTTP GET method by building a simple web * page. */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String title = "MyFirst Servlet Output"; // set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out = response.getWriter();
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}