servlet 基础之1
来源:互联网 发布:淘宝网围巾专卖 编辑:程序博客网 时间:2024/05/01 01:28
1.创建项目
用myeclipse创建项目open
2.配置tomcat
在tomcat\conf\Catalina\localhost下添加ROOT.xml:
<?xml version='1.0' encoding='utf-8'?><Context crossContext="true" docBase="项目路径\WebRoot" path="" reloadable="true"></Context>
3.在项目的WebRoot/WEB-INF/下添加web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"version="3.0"><display-name>open</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
实现最简单的HelloWorld显示功能
1).编写类HelloWorldServlet:
package com.ben.servlet;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;/** * * @author ben1 * */public class HelloWorldServlet extends HttpServlet {private static final long serialVersionUID = 5499627704074407733L;//信息private String message;public void init() throws ServletException {// 执行必需的初始化 message = "Hello World";}protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 设置响应内容类型resp.setContentType("text/html");//向页面输出结果PrintWriter out=resp.getWriter();out.print("<hr>"+message+"</hr>");}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}public void destory() {//不用任何处理}}2). 在web.xml 文件中创建以下条目
<servlet><servlet-name>HelloWorld</servlet-name><servlet-class>com.ben.servlet.HelloWorldServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloWorld</servlet-name><url-pattern>/HelloWorld</url-pattern></servlet-mapping>
3).最后在浏览器的地址栏中输入http://localhost:8080/HelloWorld。会看到下面的结果:
1 0
- servlet 基础之1
- Guice之Servlet基础
- JavaWEB基础之Servlet
- javaweb基础之--Servlet
- javaweb基础之servlet
- java web基础之servlet
- java web基础之servlet
- Java基础:Servlet之HelloWorld
- java web基础之servlet
- JavaWeb基础学习之servlet
- java web基础之servlet
- Web开发之Servlet基础
- Jsp之三 servlet基础
- javaEE基础servlet之ServletConfig
- Jsp学习之Servlet基础
- java web基础之servlet
- Web基础之Servlet深入
- javaee基础之servlet(一)
- LruCache 缓存
- LeetCode 238. Product of Array Except Self
- 阿拉伯数字转化人民币读法
- 检测字符串是否相等
- NSURLRequestCachePolicy—iOS缓存策略
- servlet 基础之1
- C语言字节对齐(以32位系统为例)
- C语言笔试经典--求分数数列的和
- Android:储存方式之SharePreferences
- 在没有安装终端服务的Windows 2012 R2服务器上配置远程桌面的属性
- 浅谈最小生成树的算法思路(一)Prim算法
- Maven学习 (一) 搭建Maven环境
- hdoj4907Task schedule【二分】
- Linux常用命令之三