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.


    单击按钮得到结果 

    目录结构为:

  • 相关阅读:
    [更新]一份包含: 采用RSA JWT(Json Web Token, RSA加密)的OAUTH2.0,HTTP BASIC,本地数据库验证,Windows域验证,单点登录的Spring Security配置文件
    fstab文件详解
    Struts2与Spring的Maven依赖冲突
    maven正确的集成命令-U -B 等
    CentOS6安装Jenkins
    CentOS6下Jenkins连接Git服务器出错的问题
    GitLab备份的创建与恢复
    开发App到上架应用市场需要经历什么?
    阅读笔记:A Few useful things to Know About machine Learning
    Feature Tools 简介
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2546940.html
Copyright © 2011-2022 走看看