Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:18:49+00:00 2026-05-31T08:18:49+00:00

I have Vector declared in index.jsp page like this: <%! Vector vNumbers = new

  • 0

I have Vector declared in index.jsp page like this:

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

I want to use it in servlet, how do I correctly get it?
Also, how do I send Vector back from servlet to JSP?
Thanks!

Joeri, no, I dont use framework.
I have the following 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);
    }

}

Here is 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:18:50+00:00Added an answer on May 31, 2026 at 8:18 am

    Note that,

    request.getAttribute() is a server-side mechanism of passing parameters between servlets and JSPs. Whereas
    request.getParameter() is normally a browser-side mechanism of passing parameters to servlets and JSPs.

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

    vNumbers has scope only within that JSP page. To pass it on to other servlets/jsps you need to set it as a request attribute.

    Inside the JSP use request.setAttribute("vector", vNumbers);
    In Servlet you can retrieve it using, Vector v = (Vector)request.getAttribute("vector");

    You could also send it as a session attribute. Similar question here

    PS: you should seek to minimize Java code in JSPs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have declared a vector in my class header like this: ... private:
I have a vector that I want to insert into a set . This
I have declared my two dimensional array like this. But getting an error due
If I have a functor like this... class DoStuff { private: std::vector < int
I have a signal in VHDL declared like this : signal Temp_Key : std_logic_vector(79
I have declared: vector<int> * part = new vector<int>[indmap]; Ok, I know it: why
I have a vector declared as a global variable that I need to be
I have a vector-like class that contains an array of objects of type T
I have a vector of integers and I want to convert it to a
I have a vector of beans that holds information I want to display in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.