Arnab Chatterjee wrote:
>
> 1) How to put a value in a session variable if the variable is supposed to
> have a value of a text     field in a JSP page?
>
> 2) How can we put an array in a session variable through JSP and access it?

If you want to place individual values in the session scope, you have to
use scriptlets, e.g.:

  <%
    session.setAttribute("myTextField", request.getParameter("myTextField");
  %>

assuming the text field in the form has the name attribute set to myTextField.
Same thing for an array:

  <%
    session.setAttribute("myCheckBoxes",
request.getParameterValues("myCheckBoxes");
    String[] myArr = new String[2];
    myArr[0] = "foo";
    myArr[1] = "bar";
   session.setAttribute("myArr", myArr);
  %>

Code like this in a page can easily create debugging and maintenance problems,
so I suggest that you instead create a bean with the appropriate properties:

  package com.mycomp.foo;
  public class MyBean {
    private String myTextField;
    private String[] myCheckBoxes;

    public String getMyTextField() {
      return myTextField;
    }

    public void setMyTextField(String value) {
      myTextField = value;
    }

    public String[] getMyCheckBoxes() {
      return myCheckBoxes;
    }

    public void setMyCheckBoxes(String[] value) {
      myCheckBoxes = value;
    }
 }

and then use <jsp:useBean> to create an instance of the bean in the session
scope and set all its properties to the submitted form values:

  <jsp:useBean id="myBean" scope="session" class="com.mycomp.foo.MyBean">
    <jsp:setProperty name="myBean" property="*" />
  </jsp:useBean>

See the JSP 1.1 spec for details about all of the above.

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to