Servlet-基础开发步骤(使用servlet接口)
2016-07-31 21:29
225 查看
本实例主要演示servlet的开发步骤。
工具:tomcat Jcreator 浏览器
1 在tomcat文件夹的webapps文件夹内新建WebFirst文件夹,接着在WebFirst下新建WEB-INF文件夹,Web-INF文件夹下新建classes文件夹以及lib文件夹,同时新建、web.xml文件
2 使用Jcreator新建java文件,并且导入servlet-api包
(1)导入jar包
(2)编写源代码,保存到之前新建的classes文件夹
编写基本java文件如下:
实现接口(如图):
实现全部源代码:
3 编写web.xml文件如下(之前新建的web.xml文件)
4 打开浏览器
输入:http://localhost:8080/WebFirst/first即可进行访问,查看到数据
工具:tomcat Jcreator 浏览器
1 在tomcat文件夹的webapps文件夹内新建WebFirst文件夹,接着在WebFirst下新建WEB-INF文件夹,Web-INF文件夹下新建classes文件夹以及lib文件夹,同时新建、web.xml文件
2 使用Jcreator新建java文件,并且导入servlet-api包
(1)导入jar包
(2)编写源代码,保存到之前新建的classes文件夹
编写基本java文件如下:
package com.tsinghua; import javax.servlet.*; import java.io.*; class HelloWord{ }
实现接口(如图):
实现全部源代码:
//first Servlet(使用接口) package com.sw; import javax.servlet.*; import java.io.*; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public class HelloWord implements Servlet{ /** * Method init * * * @param parm1 * @throws ServletException * */ //用于初始化servlet(类似于类的构造函数) //该函数只会被调用一次(当用户第一次访问时调用) public void init(ServletConfig parm1) throws ServletException { // TODO: 在这添加你的代码 System.out.println("init it"); } /** * Method getServletConfig * * * @return * */ //得到serclet配置文件 public ServletConfig getServletConfig() { // TODO: 在这添加你的代码 return null; } /** * Method service * * * @param parm1 * @param parm2 * @throws ServletException @throws IOException * */ //用于处理业务逻辑 //书写业务逻辑代码 //每次访问时都会被调用 //Request用于获得客户端的信息 //Response用于向客户端返回信息 public void service(ServletRequest parm1, ServletResponse parm2) throws ServletException, IOException { // TODO: 在这添加你的代码 //从parm2中得到printwriter System.out.println("service it"); PrintWriter pw=parm2.getWriter(); pw.println("First servlet!"); } /** * Method getServletInfo * * * @return * */ public String getServletInfo() { // TODO: 在这添加你的代码 return null; } /** * Method destroy * * */ //销毁servlet实例(释放内存) //1 重装serclet(webapps)时候 2 关闭tomcat时 3 关机时 public void destroy() { // TODO: 在这添加你的代码 System.out.println("destory!"); } }
3 编写web.xml文件如下(之前新建的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>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <servlet> <!--servlet名字--> <servlet-name>HelloWord</servlet-name> <!--sevlet(包名+类名)--> <servlet-class>com.sw.HelloWord</servlet-class> </servlet> <!--??--> <servlet-mapping> <!--??--> <servlet-name>HelloWord</servlet-name> <!--浏览器访问的url(任意)--> <url-pattern>/first</url-pattern> </servlet-mapping> </web-app>
4 打开浏览器
输入:http://localhost:8080/WebFirst/first即可进行访问,查看到数据
相关文章推荐
- Servlet之基础、开发步骤、调用过程及常见错误
- (2)使用myeclipse创建servlet步骤:继承HttpServlet开发servlet
- NHibernate0.7 发布,软件基础开发平台以及在软件基础开发平台中使用O/R Mapping
- 在VC中使用ADO的开发步骤(名字空间)
- 使用NetBeans6开发OSGi应用(4)——Servlet与Http服务[88250原创]
- Exchange开发中CDO对象的使用基础(1)
- 使用Eclipse3.0开发Servlet
- 使用MIDP2.0开发游戏 (1) GameCanvas基础
- Java中使用JCOM操作Office对象-Java基础-Java-编程开发
- 使用 Axis2 进行 SOA 开发,第 1 部分: 了解 Axis2 基础
- 使用ARM开发详细步骤
- 教案:开发基于JSPServletJavaBean的网上交易系统-第2章 Servlet基础
- 使用MIDP2.0开发游戏(1)GameCanvas基础
- asp.net控件开发基础(2) ----------明白使用Render方法呈现自定义控件
- 使用 Axis2 进行 SOA 开发,第 1 部分: 了解 Axis2 基础
- 项目开发时clearcase一般使用步骤
- 使用Eclipse和AJDT开发AspectJ应用(1)- 基础篇
- 使用ARM开发详细步骤(转自http://blog.csdn.net/zswolf2000/archive/2005/12/28/563904.aspx)
- asp.net控件开发基础(10) -- 集合属性的使用
- 使用NetBeans6开发OSGi应用(4)——Servlet与Http服务[88250原创]