Java Tutorial/Servlet/Introduction
Содержание
Get all Server Related Parameters
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet extends HttpServlet {
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
ServletContext context = getServletContext();
out.println("req.getServerName(): " + req.getServerName());
out.println("req.getServerPort(): " + req.getServerPort());
out.println("context.getServerInfo(): " + context.getServerInfo());
out.println("getServerInfo() name: " +
getServerInfoName(context.getServerInfo()));
out.println("getServerInfo() version: " +
getServerInfoVersion(context.getServerInfo()));
out.println("context.getAttributeNames():");
Enumeration e = context.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(" context.getAttribute(\"" + name + "\"): " +
context.getAttribute(name));
}
}
private String getServerInfoName(String serverInfo) {
int slash = serverInfo.indexOf("/");
if (slash == -1) return serverInfo;
else return serverInfo.substring(0, slash);
}
private String getServerInfoVersion(String serverInfo) {
// Version info is everything between the slash and the space
int slash = serverInfo.indexOf("/");
if (slash == -1) return null;
int space = serverInfo.indexOf(" ", slash);
if (space == -1) space = serverInfo.length();
return serverInfo.substring(slash + 1, space);
}
}
List All Init Parameters in Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet extends HttpServlet {
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Init Parameters:");
Enumeration e = getInitParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
out.println(name + ": " + getInitParameter(name));
}
}
}
Servlet Converter from Kilometers to Miles
/**
* Copyright (c) 2002 by Phil Hanna
* All rights reserved.
*
* You may study, use, modify, and distribute this
* software for any purpose provided that this
* copyright notice appears in all copies.
*
* This software is provided without warranty
* either expressed or implied.
*/
import java.text.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
/**
* Numeric format used to display temperatures
*/
private static final DecimalFormat FMT
= new DecimalFormat("#0.00");
/**
* Factor to convert from km/l to mi/gal
*/
private static final double CONVERSION_FACTOR = 2.352145;
/**
* Handles a GET request
*/
public void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set up for creating HTML output
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Generate heading
out.println
( "<html>"
+ "<head>"
+ "<title>Fuel Efficiency Conversion Chart</title>"
+ "</head>"
+ "<body>"
+ "<center>"
+ "<h1>Fuel Efficiency Conversion Chart</h1>"
+ "<table border="1" cellpadding="3" cellspacing="0">"
+ "<tr>"
+ "<TH>Kilometers per Liter</th>"
+ "<TH>Miles per Gallon</th>"
+ "</tr>"
);
// Generate table
for (double kmpl = 5; kmpl <= 20; kmpl += 1.0) {
double mpg = kmpl * CONVERSION_FACTOR;
out.println
( "<tr>"
+ "<td align="right">" + FMT.format(kmpl) + "</td>"
+ "<td align="right">" + FMT.format(mpg) + "</td>"
+ "</tr>"
);
}
// Generate footer
out.println
( "</table>"
+ "</center>"
+ "</body>"
+ "</html>"
);
}
}
Setup Servlet Environment
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
out.println("<BODY>");
out.println("<BIG>Hello World</BIG>");
out.println("</BODY></HTML>");
}
}
Your First Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
out.println("<BODY>");
out.println("<BIG>Hello World</BIG>");
out.println("</BODY></HTML>");
}
}