javaweb入门实例---servlet例子
1.编写servlet: TreeDataServlet.java
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
<?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>/servlet/treeServlet</url-pattern> </servlet-mapping> </web-app>
不针对此例子,例如如下:
<servlet>
<description>任意</description>
<display-name>任意</display-name>
<servlet-name>ServletDemoFirst</servlet-name>
<servlet-class>cn.dragon.servlet.ServletDemoFirst</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemoFirst</servlet-name>
<url-pattern>/servlet/ServletDemoFirst</url-pattern>
</servlet-mapping>
① 上面的两个<servlet-name>必须相同
② <servlet-class>后面指在对应的类上面. 技巧:你可以直接在你的servlet类中复制过来,这样可以避免出错!
③ <url-pattern> 必须是/servlet 再加servlet名字.大家现在就这么记.
3.测试,访问地址:http://localhost:8080/fistServlet/treeServlet
页面显示如下:
Hello~First Servlet!
自己实例:
1:编写servlet:
package xjl; 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 xjl World!"); return sb.toString(); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ // super.doGet(req, resp);一定要注释掉这句,否则会出错 String data=this.getData(); resp.setContentType("text/html;charset=GBK"); resp.getOutputStream().write(data.getBytes("GBK")); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // super.doPost(req, resp);一定要注释掉这句,否则会出错
doGet(req, resp); } }
2 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"> <servlet> <servlet-name>TreeDataServlet</servlet-name> <servlet-class> xjl.TreeDataServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>TreeDataServlet</servlet-name> <url-pattern>/servlet/xjl.TreeDataServlet</url-pattern>//注意这里url-pattern格式必须为:/servlet/包名.类名 </servlet-mapping> </web-app>
3:访问:http://localhost:8080/helloworld/servlet/xjl.TreeDataServlet
Hello xjl World!