孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

写一个html文件,做form调用

<html>
<head>
<title>Java Servelets Sample - Properties</title>
</haed>

<body>
<form METHOD="POST" ACTION="/myapp/Properties" ENCTYPE="x-www-form-encoded">
<h2><center>Java Servlets Sample - Properties</center></h2>
<hr>
<p>Press the button below to call a sample servlet that will
  return information about you,and also list the system properties
  on the server.This is done via a simple servlet.
<br><br> 
<center>
<INPUT NAME="Properties" Type="Submit" VALUE="Test Properites servlet">
</center>
<br>
<hr>
</body>
</html>

 

 

建一test目录,在此目录下编写Servelet脚本

 

package test;
import javax.servlet.*;
import javax.servlet.http.*;

public class Properties extends HttpServlet
{
	public void doPost(HttpServletRequest req,
						HttpServletResponse resp) throws ServletException,java.io.IOException
	{
		resp.setContentType("text/html");
		java.io.PrintWriter out=new java.io.PrintWriter(resp.getOutputStream());
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Java Servlets Sample-Properties</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.getRemoteHost()+"</td>");
		out.println("</tr>");
		
		out.println("<tr>");
		out.println("<td>User</td>");
		out.println("<td>"+req.getProtocol()+"</td>");
		out.println("</tr>");
		
		java.util.Enumeration enum1=req.getParameterNames();
		while (enum1.hasMoreElements()) {
			String name=(String) enum1.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("<br>");
		out.println("<Center><table border width=80%>");
		java.util.Properties props=System.getProperties();
		enum1=props.propertyNames();
		while (enum1.hasMoreElements()) {
			String name=(String) enum1.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(ServletConfig cfg) throws ServletException
	{
		super.init(cfg);
	}
	
	public void destroy()
	{
		super.destroy();
	}
}

 编译生成Properites.class

将其考入myapp\WEB-INF\classes\test中

 

改写myapp\WEB-INF\web.xml为

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<web-app>
    <display-name>My Web Application</display-name>
	<description>	
		A application for test.
	</description>	
	<servlet> 
		<servlet-name>Test</servlet-name> 
		<display-name>Test</display-name> 
		<description>A test Servlet</description> 
		<servlet-class>test.Test</servlet-class> 
	</servlet> 
	<servlet-mapping> 
		<servlet-name>Test</servlet-name> 
		<url-pattern>/Test</url-pattern> 
	</servlet-mapping> 
	
	<servlet> 
		<servlet-name>Properties</servlet-name> 
		<display-name>Server Properties</display-name> 
		<description>A Server Properties Servlet</description> 
		<servlet-class>test.Properties</servlet-class> 
	</servlet> 
	<servlet-mapping> 
		<servlet-name>Properties</servlet-name> 
		<url-pattern>/Properties</url-pattern> 
	</servlet-mapping> 
</web-app>

 启动Tomcat,在浏览器中运行http://localhost:9090/myapp/Properties.html

Java Servlets Sample - Properties

Press the button below to call a sample servlet that will return information about you,and also list the system properties on the server.This is done via a simple servlet.

 


单击按钮得到结果 

 

目录结构为:

 

 

 

posted on 2012-06-12 22:20  孤独的猫  阅读(282)  评论(0编辑  收藏  举报