【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-BeeShirtsServlet.java,希望本篇文章对您学习有所帮助。
package ejava.servletsch32;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Enumeration;
import java.sql.Connection;
import java.sql.Statement;
import java.
sql.SQLException;
import java.sql.DriverManager;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* This servlet generates the front page for BeeShirts.com
*/
public class BeeShirtsServlet extends HttpServlet
{
private ServletConfig servletConfig;
public static final String SERVLET_NAME = "/BeeShirtsServlet";
/**
* This is the first method the Web Server calls and it is called
* once.
*/
public void init (ServletConfig config)
throws ServletException
{
servletConfig = config;
}
/**
* This is the last method WebServer calls and it is called only once.
*/
public void destroy()
{
}
/**
* return the Configuration information about the Servlet
*/
public ServletConfig getServletConfig()
{
return servletConfig;
}
/**
* Return Servlet Information
*/
public String getServletInfo()
{
return "BeeShirts.com Welcome page Servlet";
}
/**
* Each time the Client calls Servlet, the service method is invoked
* once.
*/
public void service (HttpServletRequest servletRequest,
HttpServletResponse servletResponse)
throws ServletException, IOException
{
// Extract server information from HTTP request
String serverInfo = ServletsHelper.getServerInfo(servletRequest);
// Extract session information
String sessionCookie = manageSession(servletRequest);
// Create initial document type and headers
PrintWriter printWriter =
ServletsHelper.createInitialDocumentResponse(servletResponse);
// Write the body of the page to the stream
writePageBody(printWriter, serverInfo, sessionCookie);
// Close with BODY and
HTML closing tags
ServletsHelper.createClosingDocumentResponse(printWriter);
}
private String manageSession(HttpServletRequest servletRequest)
{
// Create a Session each time the user comes to this site.
HttpSession session = servletRequest.getSession(true);
// Get any cookies from servlet request
Cookie[] cookies = servletRequest.getCookies();
// Retrieve value for standard BEESHIRTS cookie if present,
// otherwise will return a null value.
String cookieValue = ServletsHelper.getOurCookieValue(cookies);
System.out.println("Cookie Value :" +cookieValue);
// If cookie value is present, then add this to the HttpSession
if(cookieValue != null){
session.setAttribute(ServletsHelper.COOKIE_NAME, cookieValue);
}
// Return cookie value if present...or null if not present
return cookieValue;
}
/**
* Write Page Body
*/
private void writePageBody(PrintWriter printWriter,
String serverInfo, String cookieValue)
{
// Write common top portion of page
ServletsHelper.writePageTopArea(printWriter);
// Partition lower page portion into one row of two TABLE columns
printWriter.println("<TABLE BORDER=0 COLS=2 WIDTH=\"100%\" >");
printWriter.println(" <TR> ");
// Write the common left-hand portion of the page
ServletsHelper.writePageLeftArea(printWriter, serverInfo);
// Write this servlet's specific center of the page info
writePageCenterArea(printWriter, cookieValue);
// Close with TABLE end tags
printWriter.println("</TR>");
printWriter.println("</TABLE> <br> ");
}
private void writePageCenterArea(PrintWriter printWriter,
String cookieValue)
{
// Write the body of the page
printWriter.println("<TD > ");
// If the cookie session value is valid, then user has visited
// the site before, so display a welcome message...
if(cookieValue != null){
printWriter.println(" <STRONG> WELCOME BACK : "
+ cookieValue +"</STRONG> <BR>");
}
String msg = "";
try{
// Get initial context and lookup welcome message via JNDI
InitialContext ctx = new InitialContext();
msg = (String) ctx.lookup("java:comp/env/WelcomeMessage");
}
catch(NamingException namingException){
namingException.printStackTrace();
}
// Print the root screen promo and welcome...
printWriter.println(msg + "</TD>");
}
}