当前位置:编程学习 > JSP >>

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

答案://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("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>

上一个:挖掘package声明的潜力 (二)
下一个:一个简单编程思想在java与php的相同实现比较!

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,