日期:2014-05-16  浏览次数:20335 次

一个例子搞懂Servlet&JSP
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	
	<servlet>
		<servlet-name>AllInOneServlet</servlet-name>
		<servlet-class>com.cdai.web.j2ee.AllInOneServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>AllInOneServlet</servlet-name>
		<url-pattern>/j2ee</url-pattern>
	</servlet-mapping>
	
	<!-- Servlet mappings END -->	

</web-app>
package com.cdai.web.j2ee;

import java.io.DataOutputStream;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class AllInOneServlet extends HttpServlet {

	public AllInOneServlet() {
		System.out.println("Servlet constructed");
	}

	@Override
	public void init() {
		System.out.println("Servlet init");
	}
	
	@Override
	public void destroy() {
		System.out.println("Servlet destory");
	}
	
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
	        throws ServletException, IOException {
		
		System.out.println("Servlet served");

		// 1.Get parameter from HTTP header
		String goWhere = request.getParameter("goto");
		
		if (goWhere == null || "1".equals(goWhere)) {
			
			// 2.Set response header
			response.setContentType("text/html");
			
			// 3.Get useful info from TCP & HTTP header
			System.out.println(
					"Request from: " + request.getRemoteAddr() + ":" + 
					request.getRemotePort() + " by method " + 
					request.getMethod());
			
			// 4.Print html(out is built-in object in JSP)
			DataOutputStream out = new DataOutputStream(response.getOutputStream());
			out.writeUTF("Hello Servlet");
			out.writeUTF("<br>");
			out.close();
		}
		else if ("2".equals(goWhere)) {
			RequestDispatcher dispather = request.getRequestDispatcher("/main.jsp?param1=java");
			request.setAttribute("param2", "servlet");
			dispather.forward(request, response);
		}
		else if ("3".equals(goWhere)) {
			response.sendRedirect("http://www.google.com?newwindow=1&q=java&oq=java");
		}
		
	}
	
}
<!-- 1.Compile instruction -->
<%@page 
	import="java.util.concurrent.atomic.*, com.cdai.web.j2ee.TestBean" 
	contentType="text/html;charset=utf-8" 
%>

<!-- 2.Declaration: member variable and method -->
<%! 

	private AtomicInteger count = new AtomicInteger(1);
	private ThreadLocal<Integer> curCountStorage = new ThreadLocal<Integer>();
	
	private int getCount() {
		int curCount = count.getAndIncrement();
		curCountStorage.set(curCount);
		return curCount;
	}
%>

<!-- 3.JSP code & 4.Built-in object -->
<%
	Object curCount = session.getAttribute("count");
	if (curCount == null) {
		curCount = getCount();
		session.setAttribute("count", curCount);
	}
	
	out.println(request.getParameter("param1") + " - " + request.getAttribute("param2"));
%>

<br> This is main.jsp. You're the <%=curCount%> visitor.

<!-- 5.Runtime action -->
<jsp:useBean id="testBean" class="com.cdai.web.j2ee.TestB