如何在servlet中使用JSP文件中的Vector?

时间:2012-03-07 10:03:10

标签: java html jsp tomcat

我在index.jsp页面中声明了Vector,如下所示:

<%! Vector vNumbers = new Vector();%>

我想在servlet中使用它,我该如何正确获取它? 另外,如何将Vector从servlet发送回JSP? 谢谢!

Joeri,不,我不使用框架。 我有以下servlet:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.FileWriter;


public class Sort extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        addNumber(request);
        goToPage("/index.jsp", request, response);
    }

 private void goToPage(String address, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
        dispatcher.forward(request, response);}


    public static void addNumber(HttpServletRequest request){

        try{
                HttpSession session = request.getSession(true);
                String path = session.getServletContext().getRealPath("/") + "numbers.txt";       
                String dataName = "textarea2";
                FileWriter writer = new FileWriter(path, true);
                writer.write(dataName + ":\n");
                writer.flush();
                writer.close();}
        catch ( Exception e) {}
  }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

}

这是index.jsp:

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%! Vector vNumbers = new Vector();%>
<HTML>
<HEAD>
<TITLE>Sorting</TITLE>
</HEAD>
<BODY>
<H1>Sorting a vector</H1>
<FORM ACTION="index.jsp" METHOD="POST">
  Input a number:
  <input type="text" name="textarea1">
  <INPUT TYPE="SUBMIT" VALUE="Enter">
</form>
<FORM ACTION="index.jsp" METHOD="POST">
  Enter amount of numbers to generate:
  <input type="text" name="textarea2">
  <input type="SUBMIT" value="Generate">
</FORM>
Your list:
<br>
 <%
  String number = request.getParameter("textarea1");
  String value = request.getParameter("textarea2");
  if (number != null) vNumbers.add(number);
   Random randomGenerator = new Random();
  if (value != null) for (int i = 0; i < Integer.parseInt(value); ++i)
  vNumbers.add(randomGenerator.nextInt(100));
  out.println(vNumbers);
       %>
<br>
<br>
<br>
</form>
<FORM ACTION="Sort.do" METHOD="POST">
  <input type="SUBMIT" value="Sort">
</FORM>
<br>
<%
String file = application.getRealPath("/") + "numbers.txt";
FileReader filereader = new FileReader(file);
BufferedReader br = new BufferedReader(filereader);
String eachLine = br.readLine();
while (eachLine != null) {
out.println(eachLine);
out.println("<br>");
eachLine = br.readLine();
}
filereader.close();       
%>
</BODY>
<HTML>

1 个答案:

答案 0 :(得分:0)

请注意,

request.getAttribute()是在servlet和JSP之间传递参数的服务器端机制。而
request.getParameter()通常是将参数传递给servlet和JSP的浏览器端机制。

<%! Vector vNumbers = new Vector();%>

vNumbers仅在该JSP页面中具有范围。要将其传递给其他servlets / jsps,您需要将其设置为请求属性。

在JSP内使用request.setAttribute("vector", vNumbers); 在Servlet中,您可以使用Vector v = (Vector)request.getAttribute("vector");

检索它

您也可以将其作为会话属性发送。类似的问题here

PS:你应该寻求最小化JSP中的Java代码。