servlet(java)第一个应用

来源:互联网 发布:php网站技术架构图 编辑:程序博客网 时间:2024/06/06 00:03

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 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_3_0.xsd"
  version="3.0"
  metadata-complete="true">


  <display-name>Tomcat Host Manager Application</display-name>
  <description>
    A scriptable host management web application for the Tomcat Web Server;
    Manager lets you view, create and remove virtual hosts.
  </description>


  <servlet>
    <servlet-name>helloworld</servlet-name>
    <servlet-class>com.kuenking.helloworld</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
  </servlet>
  <!-- Define the Manager Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>helloworld</servlet-name>
    <url-pattern>/text</url-pattern>
  </servlet-mapping>


</web-app>



///class文件夹中的 helloworld.java

///这个是第一个Servlet
package com.kuenking;


import javax.servlet.*;
import java.io.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
public class helloworld  implements Servlet{
/**
* Method init
*
*Servlet初始化;该函数只被调用一次啊!
* @param parm1
*
@throws ServletException
*
*/
public void init(ServletConfig parm1) throws ServletException {
// TODO: Add your code here
System.out.println("Init it");
}


/**
* Method getServletConfig
*
*
* @return
*
*/
public ServletConfig getServletConfig() {
// TODO: Add your code here
return null;
}


/**
* Method service
*
*
* @param parm1
* @param parm2
*这个函数应用于处理业务逻辑 程序员应当把业务逻辑代码写在这里
*当用户每次访问该Servlet时候,都会调用一次
*req:用于获得客户端的信息;
*res:应于向客户端返回信息;
@throws ServletException
@throws IOException
*
*/
public void service(ServletRequest parm1, ServletResponse parm2) throws ServletException, IOException {
// TODO: Add your code here
System.out.println("service it");
///首先从RES中得到printwriter
PrintWriter pw = parm2.getWriter();
pw.println("hello world!");
}


/**
* Method getServletInfo
*
*
* @return
*
*/
public String getServletInfo() {
// TODO: Add your code here
return "";
}


/**
* Method destroy
*
*销毁Servlet示例;(释放内存)
*reload该servlet 或者关机tomcat两个都会释放内存!
*/
public void destroy() {
// TODO: Add your code here


System.out.println("destroy!");
}


}

///打开Tomcat ,,最小化 不要关机!!!

在浏览器中输入:http://127.0.0.1:8080/myWebSite/text           (Text 是servlet中的url-pattern)httpServlet需要reload tomcat

0 0
原创粉丝点击