zoukankan      html  css  js  c++  java
  • Servelet基础

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


    单击按钮得到结果 

    目录结构为:

  • 相关阅读:
    2020年12月2日
    2020年12月1日
    2020年11月30日
    2020年11月29日
    2020年11月28日
    2020年11月27日
    2020年11月26日
    2020年11月25日
    浅谈扩展欧几里得算法
    Hello 2020
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2546940.html
Copyright © 2011-2022 走看看