Created
February 15, 2012 14:56
-
-
Save ChrisMoney/dcfcaf67e18a3cb5406b to your computer and use it in GitHub Desktop.
Java Servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java Servlet | |
import java.io.*; | |
import javax.servlet.*; | |
import java.servlet.http.*; | |
import java.util.*; | |
public class HellowServlet extends HttpServlet | |
{ | |
public void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws IOException, ServletException | |
{ | |
response.setContentType("text/html"); | |
PrintWriter out = response.getWriter(); | |
String msg = getGreeting(); | |
out.println("<html>"); | |
out.println("<head>"); | |
out.println("<title>Hello World Servlet</title>"); | |
out.println("</head>"); | |
out.println("<body>"); | |
out.println("<h1>"); | |
out.println(msg); | |
out.println("</h1>"); | |
out.println("</html>"); | |
} | |
public void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws IOException, Servlet Exception | |
{ | |
doGet(request, response); | |
} | |
private String getGreeting() | |
{ | |
String msg = "" | |
int rand = (int) (Math.random() * (6) + 1; | |
switch (rand) | |
{ | |
case 1; | |
return "Hello World!"; | |
case 2: | |
return "Greeting!"; | |
case 3: | |
return "Felicitions!"; | |
case 4: | |
return "Yo, Dude!"; | |
case 5: | |
return "Whassuuup?"; | |
case 6: | |
return "Hark!"; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment