java Servlet(1)


Web.xmlファイル:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>
    
    <init-param>
    	<param-name>encoding</param-name>
    	<param-value>GB2312</param-value>
    </init-param>
    
     <init-param>
    	<param-name>corpname</param-name>
    	<param-value>      </param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>*.ddd</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <form action="http://localhost:8080/Hello/aa.ddd" method="post">
  title:<input type="text" name="title"></input> <br/>
   content:<textarea rows="3" cols="5" name="content"></textarea> <br/>
  <input type="submit" name="  " />
  </form>
</html>

MyServlet:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

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 MyServlet extends HttpServlet {

	private ServletConfig config;
	private String encoding;
	private String corpname;
	/**
	 * Constructor of the object.
	 */
	public MyServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String url=request.getRequestURI();
		String ip=request.getRemoteAddr();
		
		/*
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
		*/
		response.setHeader("Content-Type","text/html;charset="+encoding);
		OutputStream ops=response.getOutputStream();
		ops.write(("  !"+corpname).getBytes());
		ops.write((url+"<br/>").getBytes());
		ops.write((ip+"<br/>").getBytes());
		//String path=getServletConfig().getServletContext().getRealPath("/aa.ddd");

		//ops.write((path+"<br/>").getBytes());
		//response.setHeader("refresh", "5;url=www.baidu.com");
		Enumeration enumHeaders=request.getHeaderNames();
		while(enumHeaders.hasMoreElements()){
			String headerName=(String)enumHeaders.nextElement();
			String headerValue=request.getHeader(headerName);
			ops.write((headerName+":"+headerValue+"<br/>").getBytes());
		}
		
		//      
		InputStream ips=request.getInputStream();
		ByteArrayOutputStream opps=new ByteArrayOutputStream();
		streamCopy(ips,opps);
		byte []result=opps.toByteArray();
		System.out.println(new String(result));
	}

	private void streamCopy(InputStream ips, ByteArrayOutputStream opps) throws IOException {
		// TODO Auto-generated method stub
		byte []buf=new byte[1024];
		int len=0;
		while((len=ips.read(buf))!=-1){
			opps.write(buf,0,len);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		// Put your code here
		this.config=config;
		encoding=config.getInitParameter("encoding");
		corpname=config.getInitParameter("corpname");
	}

}