Search |
||
Blarg #13: I think it is silly that your HelloWorld servlet produces static contentPosted by jfalkner on May 23, 2005 at 2:27 PM PDT
Why is it that the first servlet people teach is one that produces static content? HelloWorld.html is appropriate. HelloWorld.java is silly. The more JSP/Servlets I do, the more frustrated I get when I see others do a mediocre job of introducing the important concepts. Sure it is important to teach that code goes in WEB-INF/classes and all the other beginner's stuff, but it certainly doesn't merit a static servlet. How about we all figure out an appropriate dynamic, simple servlet for the every present I'm-coding-my-first-servlet exercise. The only thing I can think of at the moment is a Servlet that displays the date, and that is far to cheesy to end the blog with. I'll finish writing this when I come up with a slightly better idea. How about a simple hit counting filter? e.g. "HelloWorld! You've been here X times?", replacing the x with an appropriate increment. Here is the code.
package example;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloServlet extends HttpServlet {
int count = 0;
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
// increment x
count ++;
// write out some text
PrintWriter out = res.getWriter();
out.println("Hello, world! This page has been visited "+count+" times.");
out.close();
}
}
Can you think of a better example? I'll be teaching this for real. Perhaps to your next employee. You don't want your next coder making static servlets do you? Jayson Falkner »
Related Topics >>
J2EE Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|