Simple example of Servlet

MyPlanet 2002-11-14 09:33:50
I am learning JAVA. Can you tell me where I can find a simple example how to create Servlet and use it in JSP?
...全文
36 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuw 2002-11-15
  • 打赏
  • 举报
回复
不知你用什么服务,但用tomcat是最简单的了.
那里有很多例子可用:
http://localhost:8080/examples/servlets/index.html
HawaiiLeo 2002-11-15
  • 打赏
  • 举报
回复
Tomcat 中例子
C:\Tomcat4.1\webapps\examples\servlets

如helloworld.html
=====================================
Source Code for HelloWorld Example

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
alsoran 2002-11-14
  • 打赏
  • 举报
回复
www.servletforum.com
longlink 2002-11-14
  • 打赏
  • 举报
回复
use Tomcat and it will give you some example.
Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. Structure of Web Applications Section 1.3. Registering Web Applications with the Server Section 1.4. Development and Deployment Strategies Section 1.5. The Art of WAR: Bundling Web Applications into WAR Files Section 1.6. Building a Simple Web Application Section 1.7. Sharing Data Among Web Applications Chapter 2. Controlling Web Application Behavior with web.xml Section 2.1. Purpose of the Deployment Descriptor Section 2.2. Defining the Header and the Root Element Section 2.3. The Elements of web.xml Section 2.4. Assigning Names and Custom URLs Section 2.5. Disabling the Invoker Servlet Section 2.6. Initializing and Preloading Servlets and JSP Pages Section 2.7. Declaring Filters Section 2.8. Specifying Welcome Pages Section 2.9. Designating Pages to Handle Errors Section 2.10. Providing Security Section 2.11. Controlling Session Timeouts Section 2.12. Documenting Web Applications Section 2.13. Associating Files with MIME Types Section 2.14. Configuring JSP Pages Section 2.15. Configuring Character Encoding Section 2.16. Designating Application Event Listeners Section 2.17. Developing for the Clustered Environment Section 2.18. J2EE Elements Chapter 3. Declarative Security Section 3.1. Form-Based Authentication Section 3.2. Example: Form-Based Authentication Section 3.3. BASIC Authentication Section 3.4. Example: BASIC Authentication Section 3.5. Configuring Tomcat to Use SSL Section 3.6. WebClient: Talking to Web Servers Interactively Section 3.7. Signing a Server Certificate Chapter 4. Programmatic Security Section 4.1. Combining Container-Managed and Programmatic Security Section 4.2. Example: Combining Container-Managed and Programmatic Security Section 4.3. Handling All Security Programmatically Section 4.4. Example: Handling All Security Programmatically Section 4.5. Using Programmatic Security with SSL Section 4.6. Example: Programmatic Security and SSL Chapter 5. Servlet and JSP Filters Section 5.1. Creating Basic Filters Section 5.2. Example: A Reporting Filter Section 5.3. Accessing the Servlet Context from Filters Section 5.4. Example: A Logging Filter Section 5.5. Using Filter Initialization Parameters Section 5.6. Example: An Access Time Filter Section 5.7. Blocking the Response Section 5.8. Example: A Prohibited-Site Filter Section 5.9. Modifying the Response Section 5.10. Example: A Replacement Filter Section 5.11. Example: A Compression Filter Section 5.12. Configuring Filters to Work with RequestDispatcher Section 5.13. Example: Plugging a Potential Security Hole Section 5.14. The Complete Filter Deployment Descriptor Chapter 6. The Application Events Framework Section 6.1. Monitoring Creation and Destruction of the Servlet Context Section 6.2. Example: Initializing Commonly Used Data Section 6.3. Detecting Changes in Servlet Context Attributes Section 6.4. Example: Monitoring Changes to Commonly Used Data Section 6.5. Packaging Listeners with Tag Libraries Section 6.6. Example: Packaging the Company Name Listeners Section 6.7. Recognizing Session Creation and Destruction Section 6.8. Example: A Listener That Counts Sessions Section 6.9. Watching for Changes in Session Attributes Section 6.10. Example: Monitoring Yacht Orders Section 6.11. Identifying Servlet Request Initialization and Destruction Section 6.12. Example: Calculating Server Request Load Section 6.13. Watching Servlet Request for Attribute Changes Section 6.14. Example: Stopping Request Frequency Collection Section 6.15. Using Multiple Cooperating Listeners Section 6.16. The Complete Events Deployment Descriptor Chapter 7. Tag Libraries: The Basics Section 7.1. Tag Library Components Section 7.2. Example: Simple Prime Tag Section 7.3. Assigning Attributes to Tags Section 7.4. Example: Prime Tag with Variable Length Section 7.5. Including Tag Body in the Tag Output Section 7.6. Example: Heading Tag Section 7.7. Example: Debug Tag Section 7.8. Creating Tag Files Section 7.9. Example: Simple Prime Tag Using Tag Files Section 7.10. Example: Prime Tag with Variable Length Using Tag Files Section 7.11. Example: Heading Tag Using Tag Files Chapter 8. Tag Libraries: Advanced Features Section 8.1. Manipulating Tag Body Section 8.2. Example: HTML-Filtering Tag Section 8.3. Assigning Dynamic Values to Tag Attributes Section 8.4. Example: Simple Looping Tag Section 8.5. Assigning Complex Objects as Values to Tag Attributes Section 8.6. Example: Table Formatting Tag Section 8.7. Creating Looping Tags Section 8.8. Example: ForEach Tag Section 8.9. Creating Expression Language Functions Section 8.10. Example: Improved Debug Tag Section 8.11. Handling Nested Custom Tags Section 8.12. Example: If-Then-Else Tag Chapter 9. JSP Standard Tag Library (JSTL) Section 9.1. Installation of JSTL Section 9.2. c:out Tag Section 9.3. c:forEach and c:forTokens Tags Section 9.4. c:if Tag Section 9.5. c:choose Tag Section 9.6. c:set and c:remove Tags Section 9.7. c:import Tag Section 9.8. c:url and c:param Tags Section 9.9. c:redirect Tag Section 9.10. c:catch Tag Chapter 10. The Struts Framework: Basics Section 10.1. Understanding Struts Section 10.2. Setting Up Struts Section 10.3. The Struts Flow of Control and the Six Steps to Implementing It Section 10.4. Processing Requests with Action Objects Section 10.5. Handling Request Parameters with Form Beans Section 10.6. Prepopulating and Redisplaying Input Forms Chapter 11. The Struts Framework: Doing More Section 11.1. Using Properties Files Section 11.2. Internationalizing Applications Section 11.3. Laying Out Pages with Tiles Section 11.4. Using Tiles Definitions Chapter 12. The Struts Framework: Validating User Input Section 12.1. Validating in the Action Class Section 12.2. Validating in the Form Bean Section 12.3. Using the Automatic Validation Framework Developing Applications with Apache Ant Section A.1. Summarizing the Benefits of Ant Section A.2. Installing and Setting Up Ant Section A.3. Creating an Ant Project Section A.4. Reviewing Common Ant Tasks Section A.5. Example: Writing a Simple Ant Project Section A.6. Using Ant to Build a Web Application Section A.7. Example: Building a Web Application Section A.8. Using Ant to Create a WAR File Section A.9. Example: Creating a Web Application WAR File Index
没办法,文件超出上传20M限制 Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. Structure of Web Applications Section 1.3. Registering Web Applications with the Server Section 1.4. Development and Deployment Strategies Section 1.5. The Art of WAR: Bundling Web Applications into WAR Files Section 1.6. Building a Simple Web Application Section 1.7. Sharing Data Among Web Applications Chapter 2. Controlling Web Application Behavior with web.xml Section 2.1. Purpose of the Deployment Descriptor Section 2.2. Defining the Header and the Root Element Section 2.3. The Elements of web.xml Section 2.4. Assigning Names and Custom URLs Section 2.5. Disabling the Invoker Servlet Section 2.6. Initializing and Preloading Servlets and JSP Pages Section 2.7. Declaring Filters Section 2.8. Specifying Welcome Pages Section 2.9. Designating Pages to Handle Errors Section 2.10. Providing Security Section 2.11. Controlling Session Timeouts Section 2.12. Documenting Web Applications Section 2.13. Associating Files with MIME Types Section 2.14. Configuring JSP Pages Section 2.15. Configuring Character Encoding Section 2.16. Designating Application Event Listeners Section 2.17. Developing for the Clustered Environment Section 2.18. J2EE Elements Chapter 3. Declarative Security Section 3.1. Form-Based Authentication Section 3.2. Example: Form-Based Authentication Section 3.3. BASIC Authentication Section 3.4. Example: BASIC Authentication Section 3.5. Configuring Tomcat to Use SSL Section 3.6. WebClient: Talking to Web Servers Interactively Section 3.7. Signing a Server Certificate Chapter 4. Programmatic Security Section 4.1. Combining Container-Managed and Programmatic Security Section 4.2. Example: Combining Container-Managed and Programmatic Security Section 4.3. Handling All Security Programmatically Section 4.4. Example: Handling All Security Programmatically Section 4.5. Using Programmatic Security with SSL Section 4.6. Example: Programmatic Security and SSL Chapter 5. Servlet and JSP Filters Section 5.1. Creating Basic Filters Section 5.2. Example: A Reporting Filter Section 5.3. Accessing the Servlet Context from Filters Section 5.4. Example: A Logging Filter Section 5.5. Using Filter Initialization Parameters Section 5.6. Example: An Access Time Filter Section 5.7. Blocking the Response Section 5.8. Example: A Prohibited-Site Filter Section 5.9. Modifying the Response Section 5.10. Example: A Replacement Filter Section 5.11. Example: A Compression Filter Section 5.12. Configuring Filters to Work with RequestDispatcher Section 5.13. Example: Plugging a Potential Security Hole Section 5.14. The Complete Filter Deployment Descriptor Chapter 6. The Application Events Framework Section 6.1. Monitoring Creation and Destruction of the Servlet Context Section 6.2. Example: Initializing Commonly Used Data Section 6.3. Detecting Changes in Servlet Context Attributes Section 6.4. Example: Monitoring Changes to Commonly Used Data Section 6.5. Packaging Listeners with Tag Libraries Section 6.6. Example: Packaging the Company Name Listeners Section 6.7. Recognizing Session Creation and Destruction Section 6.8. Example: A Listener That Counts Sessions Section 6.9. Watching for Changes in Session Attributes Section 6.10. Example: Monitoring Yacht Orders Section 6.11. Identifying Servlet Request Initialization and Destruction Section 6.12. Example: Calculating Server Request Load Section 6.13. Watching Servlet Request for Attribute Changes Section 6.14. Example: Stopping Request Frequency Collection Section 6.15. Using Multiple Cooperating Listeners Section 6.16. The Complete Events Deployment Descriptor Chapter 7. Tag Libraries: The Basics Section 7.1. Tag Library Components Section 7.2. Example: Simple Prime Tag Section 7.3. Assigning Attributes to Tags Section 7.4. Example: Prime Tag with Variable Length Section 7.5. Including Tag Body in the Tag Output Section 7.6. Example: Heading Tag Section 7.7. Example: Debug Tag Section 7.8. Creating Tag Files Section 7.9. Example: Simple Prime Tag Using Tag Files Section 7.10. Example: Prime Tag with Variable Length Using Tag Files Section 7.11. Example: Heading Tag Using Tag Files Chapter 8. Tag Libraries: Advanced Features Section 8.1. Manipulating Tag Body Section 8.2. Example: HTML-Filtering Tag Section 8.3. Assigning Dynamic Values to Tag Attributes Section 8.4. Example: Simple Looping Tag Section 8.5. Assigning Complex Objects as Values to Tag Attributes Section 8.6. Example: Table Formatting Tag Section 8.7. Creating Looping Tags Section 8.8. Example: ForEach Tag Section 8.9. Creating Expression Language Functions Section 8.10. Example: Improved Debug Tag Section 8.11. Handling Nested Custom Tags Section 8.12. Example: If-Then-Else Tag Chapter 9. JSP Standard Tag Library (JSTL) Section 9.1. Installation of JSTL Section 9.2. c:out Tag Section 9.3. c:forEach and c:forTokens Tags Section 9.4. c:if Tag Section 9.5. c:choose Tag Section 9.6. c:set and c:remove Tags Section 9.7. c:import Tag Section 9.8. c:url and c:param Tags Section 9.9. c:redirect Tag Section 9.10. c:catch Tag Chapter 10. The Struts Framework: Basics Section 10.1. Understanding Struts Section 10.2. Setting Up Struts Section 10.3. The Struts Flow of Control and the Six Steps to Implementing It Section 10.4. Processing Requests with Action Objects Section 10.5. Handling Request Parameters with Form Beans Section 10.6. Prepopulating and Redisplaying Input Forms Chapter 11. The Struts Framework: Doing More Section 11.1. Using Properties Files Section 11.2. Internationalizing Applications Section 11.3. Laying Out Pages with Tiles Section 11.4. Using Tiles Definitions Chapter 12. The Struts Framework: Validating User Input Section 12.1. Validating in the Action Class Section 12.2. Validating in the Form Bean Section 12.3. Using the Automatic Validation Framework Developing Applications with Apache Ant Section A.1. Summarizing the Benefits of Ant Section A.2. Installing and Setting Up Ant Section A.3. Creating an Ant Project Section A.4. Reviewing Common Ant Tasks Section A.5. Example: Writing a Simple Ant Project Section A.6. Using Ant to Build a Web Application Section A.7. Example: Building a Web Application Section A.8. Using Ant to Create a WAR File Section A.9. Example: Creating a Web Application WAR File Index
Preface xxi Part I: Introduction 1 Chapter 1: Overview 3 Java EE 6 Platform Highlights 4 Java EE Application Model 5 Distributed Multitiered Applications 6 Java EE Containers 13 Web Services Support 15 Java EE Application Assembly and Deployment 17 Packaging Applications 17 Development Roles 19 Java EE 6 APIs 22 Java EE 6 APIs in the Java Platform, Standard Edition 6.0 31 GlassFish Server Tools 34 Chapter 2: Using the Tutorial Examples 37 Required Software 37 Starting and Stopping the GlassFish Server 41 Starting the Administration Console 42 Starting and Stopping the Java DB Server 43 Building the Examples 44 Tutorial Example Directory Structure 44 Getting the Latest Updates to the Tutorial 44 Debugging Java EE Applications 45 Part II: The Web Tier 47 Chapter 3: Getting Started with Web Applications 49 Web Applications 50 Web Application Lifecycle 51 Web Modules: The hello1 Example 53 Configuring Web Applications: The hello2 Example 62 Further Information about Web Applications 71 Chapter 4: JavaServer Faces Technology 73 What Is a JavaServer Faces Application? 74 JavaServer Faces Technology Benefits 75 Creating a Simple JavaServer Faces Application 77 Further Information about JavaServer Faces Technology 81 Chapter 5: Introduction to Facelets 83 What Is Facelets? 83 Developing a Simple Facelets Application 85 Templating 91 Composite Components 94 Resources 96 Chapter 6: Expression Language 99 Overview of the EL 99 Immediate and Deferred Evaluation Syntax 100 Value and Method Expressions 102 Defining a Tag Attribute Type 108 Literal Expressions 109 Operators 111 Reserved Words 111 Examples of EL Expressions 112 Chapter 7: Using JavaServer Faces Technology in Web Pages 113 Setting Up a Page 113 Adding Components to a Page Using HTML Tags 114 Using Core Tags 143 Chapter 8: Using Converters, Listeners, and Validators 145 Using the Standard Converters 145 Registering Listeners on Components 151 Using the Standard Validators 152 Referencing a Backing Bean Method 154 Chapter 9: Developing with JavaServer Faces Technology 159 Backing Beans 159 Writing Bean Properties 162 Writing Backing Bean Methods 170 Using Bean Validation 174 Chapter 10: Java Servlet Technology 179 What Is a Servlet? 180 Servlet Lifecycle 180 Sharing Information 182 Creating and Initializing a Servlet 183 Writing Service Methods 184 Filtering Requests and Responses 187 Invoking Other Web Resources 191 Accessing the Web Context 193 Maintaining Client State 193 Finalizing a Servlet 195 The mood Example Application 198 Further Information about Java Servlet Technology 200 Part III: Web Services 201 Chapter 11: Introduction to Web Services 203 What Are Web Services? 203 Types of Web Services 203 Deciding Which Type of Web Service to Use 206 Chapter 12: Building Web Services with JAX-WS 207 Creating a Simple Web Service and Clients with JAX-WS 208 Types Supported by JAX-WS 217 Web Services Interoperability and JAX-WS 217 Further Information about JAX-WS 217 Chapter 13: Building RESTful Web Services with JAX-RS 219 What Are RESTful Web Services? 219 Creating a RESTful Root Resource Class 220 Example Applications for JAX-RS 235 Further Information about JAX-RS 240 Part IV: Enterprise Beans 243 Chapter 14: Enterprise Beans 245 What Is an Enterprise Bean? 245 What Is a Session Bean? 247 What Is a Message-Driven Bean? 249 Accessing Enterprise Beans 251 The Contents of an Enterprise Bean 258 Naming Conventions for Enterprise Beans 260 The Lifecycles of Enterprise Beans 261 Further Information about Enterprise Beans 264 Chapter 15: Getting Started with Enterprise Beans 265 Creating the Enterprise Bean 265 Modifying the Java EE Application 269 Chapter 16: Running the Enterprise Bean Examples 271 The cart Example 271 A Singleton Session Bean Example: counter 278 A Web Service Example: helloservice 286 Using the Timer Service 290 Handling Exceptions 300 Part V: Contexts and Dependency Injection for the Java EE Platform 303 Chapter 17: Introduction to Contexts and Dependency Injection for the Java EE Platform 305 Overview of CDI 306 About Beans 307 About Managed Beans 307 Beans as Injectable Objects 308 Using Qualifiers 309 Injecting Beans 310 Using Scopes 310 Giving Beans EL Names 312 Adding Setter and Getter Methods 312 Using a Managed Bean in a Facelets Page 313 Injecting Objects by Using Producer Methods 314 Configuring a CDI Application 315 Further Information about CDI 315 Chapter 18: Running the Basic Contexts and Dependency Injection Examples 317 The simplegreeting CDI Example 317 The guessnumber CDI Example 322 Part VI: Persistence 331 Chapter 19: Introduction to the Java Persistence API 333 Entities 333 Entity Inheritance 345 Managing Entities 349 Querying Entities 355 Further Information about Persistence 355 Chapter 20: Running the Persistence Examples 357 The order Application 357 The roster Application 369 The address-book Application 376 Chapter 21: The Java Persistence Query Language 381 Query Language Terminology 382 Creating Queries Using the Java Persistence Query Language 382 Simplified Query Language Syntax 384 Example Queries 385 Full Query Language Syntax 390 Chapter 22: Using the Criteria API to Create Queries 415 Overview of the Criteria and Metamodel APIs 415 Using the Metamodel API to Model Entity Classes 417 Using the Criteria API and Metamodel API to Create Basic Typesafe Queries 418 Part VII: Security 427 Chapter 23: Introduction to Security in the Java EE Platform 429 Overview of Java EE Security 430 Security Mechanisms 435 Securing Containers 439 Securing the GlassFish Server 440 Working with Realms, Users, Groups, and Roles 441 Establishing a Secure Connection Using SSL 449 Further Information about Security 454 Chapter 24: Getting Started Securing Web Applications 455 Overview of Web Application Security 455 Securing Web Applications 457 Using Programmatic Security with Web Applications 469 Examples: Securing Web Applications 474 Chapter 25: Getting Started Securing Enterprise Applications 485 Securing Enterprise Beans 486 Examples: Securing Enterprise Beans 496 Securing Application Clients 504 Securing Enterprise Information Systems Applications 506 Part VIII: Java EE Supporting Technologies 511 Chapter 26: Introduction to Java EE Supporting Technologies 513 Transactions 513 Resources 514 Chapter 27: Transactions 517 What Is a Transaction? 517 Container-Managed Transactions 518 Bean-Managed Transactions 524 Transaction Timeouts 525 Updating Multiple Databases 526 Transactions in Web Components 528 Further Information about Transactions 528 Chapter 28: Resource Connections 529 Resources and JNDI Naming 529 DataSource Objects and Connection Pools 530 Resource Injection 531 Resource Adapters and Contracts 534 Metadata Annotations 538 Common Client Interface 540 Further Information about Resources 541 Index 543
restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org ----------------------------------------- Native REST support * Core REST concepts have equivalent Java classes (UniformInterface, Resource, Representation, Connector for example). * Suitable for both client-side and server-side web applications. The innovation is that that it uses the same API, reducing the learning curve and the software footprint. * Restlet-GWT module available, letting you leverage the Restlet API from within any Web browser, without plugins. * Concept of "URIs as UI" supported based on the URI Templates standard. This results in a very flexible yet simple routing with automatic extraction of URI variables into request attributes. * Tunneling service lets browsers issue any HTTP method (PUT, DELETE, MOVE, etc.) through a simple HTTP POST. This service is transparent for Restlet applications. Complete Web Server * Static file serving similar to Apache HTTP Server, with metadata association based on file extensions. * Transparent content negotiation based on client preferences. * Conditional requests automatically supported for resources. * Remote edition of files based on PUT and DELETE methods (aka mini-WebDAV mode). * Decoder service transparently decodes compressed or encoded input representations. This service is transparent for Restlet applications. * Log service writes all accesses to your applications in a standard Web log file. The log format follows the W3C Extended Log File Format and is fully customizable. * Powerful URI based redirection support similar to Apache Rewrite module. Available Connectors * Multiple server HTTP connectors available, based on either Mortbay's Jetty or the Simple framework or Grizzly NIO framework. * AJP server connector available to let you plug behind an Apache HTT

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧