Listing 2. HelloWorld.java: a Simple Applet that Handles the GET
Request Method
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// Set the MIME content type for the response
response.setContentType("text/html");
// Get the output stream to STDOUT
PrintWriter out = response.getWriter();
// Print some HTML
out.println("<HTML>");
out.println("<Head><Title>Hello,
<@cont_arrow><\#229><@$p>world</Title></Head>");
out.println("<Body>");
out.println("<P>Hello, world</P>");
// Get the user's first name, if he or
she passed it to us
String firstname = request.getParameter("firstname");
// Print a special greeting for people who
give us their name if (firstname != null)
{
out.println("<P>Your first name is \""
<@cont_arrow><\#229><@$p>+ firstname + "\"</P>");
}
// Finish the HTML
out.println("</Body></HTML>");
}
}
Copyright © 1994 - 2019 Linux Journal. All rights reserved.