to loveyourvv !servlet 传对象给jsp 的最简单的例子

发表于:2007-07-04来源:作者:点击数: 标签:
//servlet package s; import java x.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class Servlet1 extends HttpServlet { //Initialize global variables public void init() throws ServletException { } //Proc
//servlet
package s;

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

public class Servlet1 extends HttpServlet {

  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    selectbf(request,response);
  }

  public void selectbf(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        ArrayList list=new ArrayList();
        list.add("aa");
        list.add("bb");
        list.add("clearcase/" target="_blank" >cc");
        request.setAttribute("userdb",list);
        ServletContext sc = getServletContext();
        String url="/readybk.jsp";
        RequestDispatcher rd = sc.getRequestDispatcher(url);
        rd.forward(request,response);
  }

  //Clean up resources
  public void destroy() {
  }
}

//jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.ArrayList"%>
<html>
<head>
<title>
Jsp1
</title>
</head>
<body>
<h1>
JBuilder Generated JSP
</h1>
<%
ArrayList mylist =(ArrayList)request.getAttribute("userdb");
out.print(mylist.size());
%>
</body>
</html>

<!--web.xml加入,不加也行-->
    <servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>s.Servlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/servlet1</url-pattern>
  </servlet-mapping>

原文转自:http://www.ltesting.net