Java Tutorial/Servlet/Servlet Methods — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:07, 1 июня 2010
Date Servlet: doGet method
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DateServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs = request.getSession(true);
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.print("<B>");
Date date = (Date) hs.getAttribute("date");
if (date != null) {
pw.print("Last access: " + date + "<br>");
}
date = new Date();
hs.setAttribute("date", date);
pw.println("Current date: " + date);
}
}
Returns any parameters and lists server properties.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Properties extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>My First Servlet</title>");
out.println("</head>");
out.println("<h2><center>");
out.println("Information About You</center></h2>");
out.println("<br>");
out.println("<center><table border>");
out.println("<tr>");
out.println("<td>Method</td>");
out.println("<td>" + req.getMethod() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>User</td>");
out.println("<td>" + req.getRemoteUser() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Client</td>");
out.println("<td>" + req.getRemoteHost() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Protocol</td>");
out.println("<td>" + req.getProtocol() + "</td>");
out.println("</tr>");
java.util.Enumeration e = req.getParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println("<tr>");
out.println("<td>Parameter "" + name + ""</td>");
out.println("<td>" + req.getParameter(name) + "</td>");
out.println("</tr>");
}
out.println("</table></center><br><hr><br>");
out.println("<h2><center>");
out.println("Server Properties</center></h2>");
out.println("<br>");
out.println("<center><table border width=80%>");
java.util.Properties props = System.getProperties();
e = props.propertyNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println("<tr>");
out.println("<td>" + name + "</td>");
out.println("<td>" + props.getProperty(name) + "</td>");
out.println("</tr>");
}
out.println("</table></center>");
out.println("</html>");
out.flush();
}
public void init() throws ServletException {
ServletConfig config = getServletConfig();
}
public void destroy() {
}
}
Using Servlet Init Method
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
String msg = "";
public void init(ServletConfig config)
{
msg = "Hello from Java servlets!";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>");
out.println("Using the init Method");
out.println("</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H1>Using the init Method</H1>");
out.println(msg);
out.println("</BODY>");
out.println("</HTML>");
}
}