① 编写Servlet并编译成class文件,编译时,将servlet-api.jar文件(在/common/lib 目录下)加入到classpath中;
② 将编译好的class文件拷贝到应用的WEB-INF/classes/下(如/myapp/WEB-INF/classes/);
③ 在web.xml中进行配置 <servlet> 、<servet-mapping>,具体可参见下面的web.xml。
这里引用myeclipse默认生成的servlet实例:
servlet:
package com.unilay.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
/**
* Constructor of the object.
*/
public Test() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>com.unilay.test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/servlet/Test</url-pattern>
</servlet-mapping>
</web-app>
然后在浏览器中运行http://127.0.0.1:8080/Test/servlet/Test
分享到:
相关推荐
基于javaee+servlet+js+html实现外卖点餐系统 基于javaee+servlet+js+html实现外卖点餐系统 基于javaee+servlet+js+html实现外卖点餐系统 基于javaee+servlet+js+html实现外卖点餐系统 基于javaee+servlet+js+html...
javaEE servlet-apiServlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面
基于javaee+servlet+js+html实现外卖点餐系统源码+数据库.zip基于javaee+servlet+js+html实现外卖点餐系统源码+数据库.zip基于javaee+servlet+js+html实现外卖点餐系统源码+数据库.zip基于javaee+servlet+js+html...
该资源是韩顺平javaEE之servlet部分讲解视频的配套源码文件,里面有详尽的注释和使用说明。
javaEE servlet 学习笔记 jsp本质上是servlet,但是为了更加符合mvc的框架,将页面显示和逻辑控制分离,jsp页面只负责页面,也就是mvc中的V(view),而servlet负责mvc中的C(control)。 而为了更加好的理解结构,一下...
JavaEE 6Servlet 3.0 中的新特性 • 易于开发和部署 • 模块化web.xml • 动态配置 • 异步Servlet • Servlet 3.0 — 易于开发 – 主要关注点 • 增强了API 以便使用SE 5 中新的语言特性 – 例如:批注、泛型...
JavaEE Servlet登录例子JavaEE Servlet登录例子JavaEE Servlet登录例子JavaEE Servlet登录例子JavaEE Servlet登录例子JavaEE Servlet登录例子JavaEE Servlet登录例子
javaEE-servlet留言板项目demo,简单易懂,内附数据库和说明文档; 适合新手学习,亲测可用。
javaee总结 Servlet 三层架构
APACHE 对 JAVAEE 标准中的 Servlet JSP 实现的源码
这是对javaee 中servlet编程中涉及到的类的总体布局联系图,有了它可以帮你清楚的了解servlet的总体布局。希望能够帮到你!
javaEE/servlet/jsp api;参考文档
这两个软件包的同时存在使得JavaEE Servlet API能够适应将来的其他请求-响应的协议。 这份文档以及刚才提及的Javadoc格式的文档都描述了这两个软件包,Javadoc格式的文档还描述了你应该如何使用这两个软件包中的所有...
JavaEE 5 Servlet 中文参考,web开发人员可以参考,希望对你有帮助。
资源名字:基于javaEE+原生Servlet+MySql的村镇旅游网站设计与实现(源码+文档)_JAVAEE_Servlet_旅游网站 资源类型:项目全套源码+文档+辅导视频 源码说明: 全部项目源码都是经过测试校正后百分百成功运行。 适合...
4.6javaEE_原生Servlet_MySql酒店管理系统的设计与实现.zip
jsp本质上是servlet,但是为了更加符合mvc的框架,将页面显示和逻辑控制分离,jsp页面只负责页面,也就是mvc中的V(view),而servlet负责mvc中的C(control)。