`
omygege
  • 浏览: 1386688 次
文章分类
社区版块
存档分类
最新评论

Servlet之Core Servlet

 
阅读更多

/*1、Web Programming Model*/
In software engineering,a Web application is an application that is accessed via web over a network such as the internet or an intranet.
The web programming model is made up of the following three factors:
Web Client
A software program used to access web pages.Sometimes the same as a Web Browser.
Web Server
A server is a computer that delivers services or information to other computers.In web terms:A server that delivers web content to web browers;
Communication Protocol:
A standard(language and a set of rules)to allow computers to interact in a standard way.HTTP is used as ths protocol in the web.
Contents generated by the server may both static and dynamic:
Static Contents
static contents are pre-created and stored on the server,they are the same for each web client;
Dynamic Contents
dynamic contents are contents generated by some programs on the server.The program can be CGI,Servlets and so on.To run such programs.the server must provide some add-in program called contrainer.

/*2、Install and configure Tomcat*/
Dowload Tomcat
you can download tomcat from http://tomcat.apache.org/ after download.unzip it to some directory on the disk.
Install JDK
JDK must be installed when building web application using Java.You can download it from http://java.sun.com
Setup environment variables
JAVA_HOME=root directory of JDK
CATALINA_HOME= root directory of tomcat
Verify server setup
After server startup,visit the following address http://127.0.0.1:8080 or http://localhost:8080

/*3、Struture of web application*/
Web application
A Web application is a collection of servlets.HTML pages,classes and other resources that make up a complete application on a Web server.The Web application can be bundled and run on multiple containers from muliple vendors.
Directory Structure
webroot--public --sub folders
--WEB-INF--web.xml
--classes
--lib
webroot
a user defined name of the web application
public
user defined folder or file names,it together with its subfolders are public to web clients.
WEB-INF
used by web server,not public to web clients.
classes
directory for servlets and utility classes.
lib
jars containing servlets and utility classes.
web.xml
The Web application deployment descriptor.Servlets,Filters and Listeners are deployed by adding configuration entries in the file.
Deploy web application in tomcat
to deploy a web application in tomcat,one should put a pre-created web application structure under the directory of $CATALINA_HOME/webapps
Visit the Web application:
http://serverip:port/webroot/resourcepath

/*4、Steps to develop Servlets*/
Steps to develop Servlets:
1.Write Servlet class and other classes it uses.
2.Compile Servlet and put class files to the classes directory of some web application.
3.Deploy Servlet by adding configration entires in web.xml
4.Restart web server and test servlet.

1.Write Servlet class and other classes it uses.
All java Servlets must implement javax.servlet.Servlet interface and provide lifecycle methods and other methods.these methods include:
init
service
destory
getServletConfig
getServletInfo
The init,service,destory mothod are called by the Container.They are lifecycle methods.
//MyServlet.java
package com.chenzw.testServlet;
import javax.servlet.*;
import java.io.*;
public class MyServlet implements Servlet{
private ServletConfig config;
public void init(ServletConfig config)throws ServletException{
this.config=config;
}
public ServletConfig getServletConfig(){
return config;
}
public void destory(){
}
public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException{
PrintWriter out=response.getWriter();
out.println("Hello Servlet!!");
out.close();
}
public String getServletInfo{
return "It's chenzw's Servlet!";
}
}
2.Compile Servlet and put class files to the classes directory of some web application.
To compile Servlets,you must get Servlet depended class files and make it available to your compilation environment.the class files can be found at:$CATALINA_HOME/common/lib/servlet-api.jar
3.Deploy Servlet by adding configuration entires in web.xml
A servlet should be configured to serve some url,so as to let web clients visit it.this is done by adding configuration entries in web.xml.
//Configuration entries in web.xml
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.chenzw.testServlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/mySerlvet<url-pattern>
</servlet-mapping>
4.Restart web server and test servlet
After restart web server,visit the following address using your web browers:http://localhost:port/myServlet
What if you develop more servlets?
public class MyServlet extends GenericServlet{
public void service(ServletRequest resquest,ServletResponse response)throws ServletExcetpion,IOException{}
}
What if you deal differently with HTTP GET and POST request?
public class MyServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{}
}

分享到:
评论

相关推荐

    jersey-container-servlet-core-2.22.2-API文档-中文版.zip

    赠送jar包:jersey-container-servlet-core-2.22.2.jar; 赠送原API文档:jersey-container-servlet-core-2.22.2-javadoc.jar; 赠送源代码:jersey-container-servlet-core-2.22.2-sources.jar; 赠送Maven依赖信息...

    jersey-container-servlet-core-2.22.2-API文档-中英对照版.zip

    赠送jar包:jersey-container-servlet-core-2.22.2.jar; 赠送原API文档:jersey-container-servlet-core-2.22.2-javadoc.jar; 赠送源代码:jersey-container-servlet-core-2.22.2-sources.jar; 赠送Maven依赖信息...

    CORE SERVLET AND JSP(含代码,基础卷1)

    《CORE SERVLET AND JSP(含代码,基础卷1)》是一本专注于Servlet和JSP技术的经典教程,尽管出版时间较早,但其深入浅出的讲解方式和丰富的实例仍然具有很高的学习价值。Servlet和JSP是Java Web开发中的核心组件,...

    S06-tomcat之servlet内存马1

    "S06-tomcat之servlet内存马1" 在 Tomcat 中,Servlet 内存马是一种特殊的 Servlet,它可以动态注册 Tomcat 组件,实现内存马攻击。下面将对 Servlet 内存马的工作原理和实现机制进行详细的分析和解释。 Servlet ...

    javax/servlet/jsp/jstl/core/ConditionalTagSupport

    在Java Web开发中,`javax.servlet.jsp.jspl.core.ConditionalTagSupport`是JSTL(JavaServer Pages Standard Tag Library)库中的一个核心类,用于支持条件标签的实现。当你遇到`java.lang.NoClassDefFoundError: ...

    用Servlet实现的二维码图片生成

    &lt;artifactId&gt;core &lt;version&gt;3.4.1 &lt;groupId&gt;com.google.zxing&lt;/groupId&gt; &lt;artifactId&gt;javase &lt;version&gt;3.4.1 ``` 然后,编写Servlet来处理请求。当用户访问特定URL时,Servlet将接收到请求,使用ZXing库...

    Core servlet jsp 中文

    《Core Servlet JSP 中文》是一个深入讲解Servlet和JSP技术的教程,针对初学者和进阶开发者提供全面的知识指导。Servlet是Java Web开发中的基础组件,用于处理HTTP请求并生成动态内容;而JSP(JavaServer Pages)则...

    jsp页面中获取servlet请求中的参数的办法详解

    &lt;servlet-class&gt;org.openjweb.core.servlet.BBSServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;bbs&lt;/servlet-name&gt; &lt;url-pattern&gt;/bbs &lt;/servlet-mapping&gt; ``` 这里的`servlet-class`...

    jsp+jstl+servlet所需要的全部包

    - **Core库**:包含基本的控制结构,如迭代和条件语句。 - **fmt库**:处理日期、时间、数字格式化。 - **fn库**:提供一些通用的函数,如字符串操作。 - **sql库**:支持数据库查询。 - **xml库**:处理XML...

    servlet3.0相关的包

    JSTL包含多个核心标签集,如Core、XML、SQL和Function,可以处理常见的页面操作,如条件判断、循环、国际化等。 4. **jstl-impl**: 这是JSTL的实现包,包含了JSTL的具体实现,使得开发者可以在实际项目中使用JSTL的...

    Servlet与JSP核心编程(第2卷 第2版)

    - JSTL提供了一组标准标签库,如Core、XML、JDBC等,增强JSP的功能和可读性。 8. **Servlet与JSP的交互** - JSP通常作为表现层,Servlet负责业务逻辑。Servlet处理请求后,可以设置请求和响应对象的属性,然后...

    servlet源码

    4. **Servlet的加载和管理**:`org.apache.catalina.loader.WebappClassLoader`负责加载Web应用的类,`org.apache.catalina.core.StandardWrapper`管理单个Servlet的实例化和初始化。 5. **servlet的映射**:在`org...

    json servlet

    例如,使用Jackson,你需要在项目中添加`jackson-databind`、`jackson-core`和`jackson-annotations`的jar包。描述中提到的"jar包"可能就是指这些依赖。 接下来,我们来创建一个简单的Servlet JSON示例。首先,定义...

    Core Servlet and Jave Server Pages

    《核心Servlet与Java服务器页面》是一本专注于Web开发的经典教程,涵盖了Servlet和JSP(Java Server Pages)的核心概念和技术。这两个技术是Java EE平台的重要组成部分,用于构建动态、交互式的Web应用程序。 ...

    servlet基础知识新手下载

    Servlet是Java Web开发中的核心组件之一,用于处理客户端请求并生成动态响应。了解其基础知识、生命周期以及如何正确配置和使用,对于开发高质量的Web应用程序至关重要。同时,掌握Request与Response对象的使用,...

    Java、Servlet创建二维码

    &lt;artifactId&gt;core &lt;version&gt;3.4.1 &lt;groupId&gt;com.google.zxing&lt;/groupId&gt; &lt;artifactId&gt;javase &lt;version&gt;3.4.1 ``` 接下来,我们要创建一个Servlet来处理生成二维码的请求。以下是一个简单的Servlet示例: `...

    java servlet使用JSON所需的所有jar包

    在Java Servlet中使用JSON(JavaScript Object Notation)技术,我们需要依赖一些特定的库来解析、生成和处理JSON数据。JSON是一种轻量级的数据交换格式,它使得服务器与客户端之间能够简单、快速地交换数据。以下是...

    jakarta.servlet.jsp.jstl-api-2.0.0.jar

    1. **Core Library**(c:):提供了基本的控制流和数据处理功能,如条件语句(if, choose, when, otherwise)、迭代(forEach, forTokens)、URL重写(redirect, url)等。 2. **Format/Internationalization ...

    Servlet与JSP核心编程第二版5

    - **Core**:常用标签,如迭代、条件判断等。 - **SQL**:数据库操作。 - **XML**:XML 处理。 - **Functions**:实用函数。 #### 三、Servlet与JSP的交互 ##### 3.1 数据共享 Servlet 和 JSP 之间可以通过请求...

    javaservlet demo jstl

    &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; ``` 3. 使用JSTL标签:在JSP页面中,使用前缀(如"c"或"fmt")加上相应的JSTL标签,如`&lt;c:forEach&gt;`、`&lt;fmt:formatDate&gt;`。 总的来说,Java...

Global site tag (gtag.js) - Google Analytics