Servelet基础

使用JavaServlet创建并调用服务器属性的示例

写一个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.

 


单击按钮得到结果 

 

目录结构为:

 

 

 

转载于:https://www.cnblogs.com/djcsch2001/archive/2012/06/12/2546940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值