servlet+jsp实例demo

经常有时候要用servlet快捷开发一些小的web项目

记录下自己常用的容易忘记的配置方法:

(我不讨论理论方面的,需要解释的问度娘了)

1、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>
       <description>This is the description of my J2EE component</description>
       <display-name>This is the display name of my J2EE component</display-name>
       <servlet-name>RaqUtil</servlet-name>
       <servlet-class>com.sdic3.elasticUtil.RaqServlet</servlet-class>
     </servlet>
     <servlet-mapping>
       <servlet-name>RaqUtil</servlet-name>
       <url-pattern>/raq.do</url-pattern>
     </servlet-mapping>
</web-app>
View Code

2、servlet

package com.sdic3.elasticUtil;

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 RaqServlet extends HttpServlet {
    //初始化
    public void init() throws ServletException {
        System.out.println("我是init()方法!用来进行初始化工作");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse rsp)
            throws ServletException, IOException {
        String login_fail = "fail.jsp";*/
        String ss = "sdic3/jsp/ass20130515.jsp";
        rsp.sendRedirect(ss2);//重定向到apage.jsp 
    }

    //销毁实例
    public void destroy() {
        super.destroy();
        System.out.println("我是destroy()方法!用来进行销毁实例的工作");
    }
}
View Code

然后用raq.do 请求就可以看到跳转了

可以参考这里这一篇文章理解servlet的常见请求处理:http://developer.51cto.com/art/200902/109939_1.htm

 

posted @ 2013-05-30 20:05  夏远  Views(340)  Comments(0Edit  收藏  举报
Hit Counter
Americas Department Store