java基础
1.编写servlet: TreeDataServlet.javaview plaincopyprint?
[*]package com.maggie.tree;
[*]
[*]import java.io.IOException;
[*]
[*]import javax.servlet.ServletException;
[*]import javax.servlet.http.HttpServlet;
[*]import javax.servlet.http.HttpServletRequest;
[*]import javax.servlet.http.HttpServletResponse;
[*]
[*]public class TreeDataServlet extends HttpServlet{
[*]
[*] private String getData(){
[*] StringBuffer sb = new StringBuffer();
[*] sb.append("Hello~First Servlet!");
[*] return sb.toString();
[*] }
[*]
[*] @Override
[*] protected void doGet(HttpServletRequest req, HttpServletResponse resp)
[*] throws ServletException, IOException {
[*] // TODO Auto-generated method stub
[*]// super.doGet(req, resp);
[*] String data = this.getData();
[*] resp.setContentType("text/html;charset=GBK");
[*] resp.getOutputStream().write(data.getBytes("GBK"));
[*] }
[*]
[*] @Override
[*] protected void doPost(HttpServletRequest req, HttpServletResponse resp)
[*] throws ServletException, IOException {
[*] // TODO Auto-generated method stub
[*]// super.doPost(req, resp);
[*] }
[*]}
2.配置web.xml
view plaincopyprint?
[*]<?xml version="1.0" encoding="UTF-8"?>
[*]<web-app version="2.5"
[*] 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_2_5.xsd">
[*] <servlet>
[*] <servlet-name>treeServlet</servlet-name>
[*] <servlet-class>
[*] com.maggie.tree.TreeDataServlet
[*] </servlet-class>
[*] <load-on-startup>1</load-on-startup>
[*] </servlet>
[*] <servlet-mapping>
[*] <servlet-name>treeServlet</servlet-name>
[*] <url-pattern>/treeServlet</url-pattern>
[*] </servlet-mapping>
[*]</web-app>
3.测试,访问地址:http://localhost:8080/fistServlet/treeServlet
看看。学习学习。挺不错的
页:
[1]