Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

jsp how to get more one selected items in the <select> tag

Posted on 2005-04-05
42
Medium Priority
?
378 Views
Last Modified: 2010-04-01
Hello, experts,

In a form of html, there is a <select> tag,  I want  user can select more than one items at one time.

i.e.
<form action ="listFiles.jsp method=post>
<select name=fileNames >
<option value=file1>file 1
<option value=file2>file 2
<option value=file3>file 3
</select>
<input type="submit" value="ListFiles" >

Q1. How to implement to allow user choose more than files in the html
Q2. How to retrieve them in the listFiles.jsp

A simple example will be very helpful,
Appreciate your attention.

         

0
Comment
Question by:lilyyan
  • 15
  • 15
  • 12
42 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 600 total points
ID: 13710436
<form action ="listFiles.jsp" method="post">
    <select multiple size="4" name="fileNames">
        <option value=file1>file 1
        <option value=file2>file 2
        <option value=file3>file 3
    </select>
    <input type="submit" value="ListFiles">
</form>

then in the JSP:

  String[] names = request.getParameterValues( "fileNames" );

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13710456
Then the variable "names" will be eithe null (if none were selected), or an array of the ones that were selected :-)

Good luck!

Tim
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13711135
In the "select"  you have to specify MULTIPLE to allow the user to select multiple values
<form action ="listFiles.jsp" method="post">
    <select multiple size="4" name="fileNames" MULTIPLE>
        <option value=file1>file 1
        <option value=file2>file 2
        <option value=file3>file 3
    </select>
    <input type="submit" value="ListFiles">
</form>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:koppcha
ID: 13711143
you can retrieve them  as suggested by Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13712161
>>  In the "select"  you have to specify MULTIPLE to allow the user to select multiple values

I said that too ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13712165
infact it's almost identical to my post :-(
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13712361
ohh ok i didnt notice that.. that is the reason why i added one more MULTIPLE If you can see that :)
0
 

Author Comment

by:lilyyan
ID: 13713461
Hi, thanks for all your reply.

sry for a little late. well, i saw TimYates's post first , and just didn't get a chance to test it. :-)

now, it works fine, but have another question need to ask for.

if i delcare the names array like this:
<%!
String[] names = request.getParameterValues( "fileNames" );
%>
-----------------------
it will give me an error:
Generated servlet error:
request cannot be resolved

......

if i delcare the names array
<%!
String[] names ;
%>

<%
names = request.getParameterValues( "fileNames" );
%>

then no error.

0
 
LVL 8

Expert Comment

by:koppcha
ID: 13713496
<%!  -->This is for declarations
<%  -->Scriptlets for regular java code
<%= -->expressions are used to print on to JSP

><%!
String[] names = request.getParameterValues( "fileNames" );
%>
In this case you are using more to declaration which is not allowed

so you can this(using scriptlets)
<%
String[] names = request.getParameterValues( "fileNames" );
%>

ans expressions like this in JSP
Name1: <%= names[1] %>
which will print the value of names[1] .Assumin it is EE the output will be

Name1: EE



0
 

Author Comment

by:lilyyan
ID: 13713575
Hi,koppcha
thanks for your prompt reply.
the array names need to be class wide variable, so i declared the array names:
<%!
String[] names ;
%>

is there another way to achieve this ?
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13713672
<% String[] names = request.getParameterValues( "fileNames" ); %> will do

What ever you keep in the JSP with <%! or <% or <%= is one whole servlet.JSP  will be converted to a servlet at a later stage.
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 600 total points
ID: 13713695
This small notes that i have should make everything clear.

There are three kinds of scripting elements:

declarations
expressions
scriplets

 
Declarations
Declarations are used to define variables and methods -- both instance and static -- that are likely to be repeated.  Thus, they correspond to writing code at the Java Class level. They can also be used to overwrite jspInit or jspDestroy methods that are created when the JSP is compiled.

The basic form is:

<%! Java variable or method %>
Example:

<%! String message; %>

or
<% String message = "Hello, World, from JSP"; %>

 Expressions
Expressions are references to variables, methods, or composable structures of such.  They are most often used as a way of dynamically inserting a value into a parameter slot. You may also think of them as representing a call to a static method.  They are evaluated at run time and the results inserted into the output stream at the location of the expression.

The basic form is:

<%= Java expression %>
Example:

<h2><font color="#AA0000"><%= message%></font></h2>
Note that expressions do not include a semicolon(;).

Scriplets
Scriplets are sections of Java code that are executed in place.  They can be as simple as a declaration of a variable that is treated as an instance variable, but they can also include loops that mix Java and HTML.

The basic form is:

<% Java code %>
0
 

Author Comment

by:lilyyan
ID: 13713719
Hi, koppcha

i just did a test. your answer is correct. i.e. the array names can be accessed outside the <% String[] names = request.getParameterValues( "fileNames" ); %> block.

then what's the difference for delcaring a variable by <%!  
or <%

i did remeber that <%! is used for delcaring class wide varable , and <% can be used for declaring local variable.

maybe the jsp edition matters ? i'm using jakarta-tomcat-5.5.7
0
 

Author Comment

by:lilyyan
ID: 13713749
Hi,koppcha ,
many thanks for your reply.

may you give out an example for declarations <%! %> by declaring a method?
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13713879
<%! --> It is for static declaration  when JSP is converted to servlet  this part goes right before service() methods.

<% --->It is to generate dynamic content. when JSP is converted to servlet  this part goes right into the service() methods.

>may you give out an example for declarations <%! %> by declaring a method?
<%!

public void temp()
{
System.out.println("koppcha");//It prints in the logs not on the JSP page just for example
}

%>
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 600 total points
ID: 13715316
Here's an example of what koppcha is saying:

-----------------------

<%!
    // DECLARATION --  we can do classes an function here...

    class TimsClass
    {
        int a, b ;
        public TimsClass( int a, int b ) { this.a = a ; this.b = b ; }
        public String toHTML() { return "<H1>" + a + "," + b + "</H1>" ; }
    }

    public TimsClass getInstance( int a, int b )
    {  
        return new TimsClass( a, b ) ;
    }
%>

<HTML>
    <HEAD>
        <TITLE>Tims cool test</TITLE>
    </HEAD>
    <BODY>
<%
    // SCRIPTLET -- This is the stuff that's executed when the page is called by a client
    for( int i = 0 ; i < 10 ; i++ )
    {
        TimsClass c = getInstance( i, 0 ) ;
        out.println( c.toHTML() ) ;
    }
%>
    </BODY>
</HTML>
0
 

Author Comment

by:lilyyan
ID: 13716763
Hi, thanks all for your good answers :)

one more question, is there a way to define a local variable in jsp. i.e. it's only valid within one <% %>


0
 
LVL 8

Expert Comment

by:koppcha
ID: 13716832
1>keep that in a function inside <% %>
2>You can declare once and reinitialize wher ever you want it to.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13716871
>> is there a way to define a local variable in jsp. i.e. it's only valid within one <% %>

Put your code in a block like:

<%
    {  // start a block...keeps these vars local
        int a = 1 ;
        int b = 2 ;
    }
%>

HTML

<%
    out.println( a ) ; // THIS will throw an error, as "a" is only local to the block above :-)
%>
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 600 total points
ID: 13716881
And if you use that, the vars will only be visible inside the { and } tags, so:

<%
    {  // start a block...keeps these vars local
        int a = 1 ;
        int b = 2 ;
        out.println( a + ", " + b ) ; // works fine
    }
    out.println( a ) ; // THIS will throw an error, as "a" is only local to the block above :-)
%>
0
 

Author Comment

by:lilyyan
ID: 13717109
so there is no difference , when using <% or <%! to declare a variable ?
0
 

Author Comment

by:lilyyan
ID: 13717149
hi, TimYates , just saw your post , thanks too :)

is the syntax valid?  just use { }, and there is no method name ?
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 600 total points
ID: 13717169
nope...

if you declare the variable in <%! it will be accessable to all functions defined in <%! too...

This is what happens...  All of the JSP gets converted to java code, and put inside a function.  So imagine a simple JSP like this:

<HTML>
  <BODY>
<% out.println( "Hi" ) ; %>
  </BODY>
</HTML>

That get converted to: (NOT ANYTHING LIKE THIS -- but similar) ;-)

class simplejsp
{
    public void doPage( Writer output )
    {
        output.print( "<HTML>" ) ;
        output.print( "<BODY>" ) ;
        output.print( "Hi" ) ;
        output.print( "</BODY>" ) ;
        output.print( "</HTML>" ) ;
    }
}

Now, when you define things in <%! they get put outside the doPage method like:

<HTML>
  <BODY>
<%! int a = 4 ; %>
<% out.println( "Hi" ) ; %>
  </BODY>
</HTML>

would become like:

class simplejsp
{
    int a = 4 ;

    public void doPage( Writer output )
    {
        output.print( "<HTML>" ) ;
        output.print( "<BODY>" ) ;
        output.print( "Hi" ) ;
        output.print( "</BODY>" ) ;
        output.print( "</HTML>" ) ;
    }
}

See?

Hope this clears up any confusion...

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13717174
So declaring a variable inside <%! would make it visible to all functions declared in <%!
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13717214
declaring a variable inside <% is visible only in the service function.
 
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13717251
>> is the syntax valid?  just use { }, and there is no method name ?


Yes  that's perfectly valid :-)
0
 

Author Comment

by:lilyyan
ID: 13717362
hi, very apprecaite your replies.

well , yesterday i did a test,
<%!
String[] names;
%>

<%
names =  request.getParameterValues( "fileNames" );
%>
----------------------
will has the same effect with :
<%
String[] names=  request.getParameterValues( "fileNames" );
%>
------------------

i.e.  i can access both in another <% %>
<%
for (int i=0; i<names.length; i++)
{
out.println("the element is : "+ names[i]);
}

%>

0
 
LVL 35

Expert Comment

by:TimYates
ID: 13717446
Yes, because this:

----------------

<%!
String[] names;
%>
<%
names =  request.getParameterValues( "fileNames" );
%>
   <b>Hello</b>
<%
for (int i=0; i<names.length; i++)
{
out.println("the element is : "+ names[i]);
}
%>

----------------

gets compiled into something (kinda) like this:

----------------

class simplejsp
{
    String[] names ;

    public void doPage( Writer output )
    {
        names =  request.getParameterValues( "fileNames" );
        output.print( "<b>hello</b>" ) ;
        for (int i=0; i<names.length; i++)
        {
            output.print( "the element is : "+ names[i] ) ;
        }
    }
}

----------------

Wheras this:

----------------

<%
String[] names=  request.getParameterValues( "fileNames" );
%>
    <b>hello</b>
<%
for (int i=0; i<names.length; i++)
{
out.println("the element is : "+ names[i]);
}
%>

----------------

gets compiled into something (kinda) like this:

----------------

class simplejsp
{
    public void doPage( Writer output )
    {
        String[] names =  request.getParameterValues( "fileNames" );
        output.print( "<b>hello</b>" ) ;
        for (int i=0; i<names.length; i++)
        {
            output.print( "the element is : "+ names[i] ) ;
        }
    }
}

----------------

You see the difference?

That is how JSPs work...

Again, the code I have shown you is NOT how it will look when Tomcat does it, as Tomcats code has LOADS of extra stuff it does...

I'm trying to show simple examples :-)

Tim
0
 

Author Comment

by:lilyyan
ID: 13717717
okay, then what is the service() methods in jsp? :)
0
 

Author Comment

by:lilyyan
ID: 13717841
>> <%! --> It is for static declaration  when JSP is converted to servlet  this part goes right before service() methods.
-by koppcha

update :  the question is what are the service() methods in the servlet that a jsp is converted to ?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13717857
How many questions is this question going to involve? ;-)

what do you mean by service() methods?

ALL servlets (by definition) have a service method

This method calls doGet or doPost depending on what is in the request headers...and some other stuff...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13717864
Ahhh...

In my rubbish example I called it

    public void doPage( Writer output )

in reality, this is a method called service()
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13717899
Write a simple jsp alled "a.jsp"

Put the code you want to see as java

go to the jsp:

http://localhost:8080/a.jsp

then look in

    Tomcat/work/Catalina/localhost/ROOT/org/apache/jsp

there should be a_jsp.java

have a look at that...that is what Tomcat converts your JSP to, and what it compiles then runs...
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 600 total points
ID: 13718192
This is a very good example that shows what is the difference between declarations and scriptlets basically declarations will only be evaluated only when the page is loaded but with scriptlets that is not the case see the examples below


To add a declaration, you must use the <%! and %> sequences to enclose your declarations, as shown below.

<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
    Date theDate = new Date();
    Date getDate()
    {
        System.out.println( "In getDate() method" );
        return theDate;
    }
%>
Hello!  The time is now <%= getDate() %>
</BODY>
</HTML>
The example has been created a little contrived, to show variable and method declarations.
Here we are declaring a Date variable theDate, and the method getDate.  Both of these are available now in our scriptlets and expressions.

But this example no longer works!  The date will be the same, no matter how often you reload the page.  This is because these are declarations, and will only be evaluated once when the page is loaded!  (Just as if you were creating a class and had variable initialization declared in it.)

Exercise:  Modify the above example to add another function computeDate which re-initializes theDate.  Add a scriptlet that calls computeDate each time.

Note: Now that you know how to do this -- it is in general not a good idea to use variables as shown here. The JSP usually will run as multiple threads of one single instance. Different threads would interfere with variable access, because it will be the same variable for all of them. If you do have to use variables in JSP, you should use synchronized access, but that hurts the performance. In general, any data you need should go either in the session objet or the request objectc (these are introduced a little later) if passing data between different JSP pages. Variables you declare inside scriptlets are fine, e.g. <% int i = 45; %> because these are declared inside the local scope and are not shared.
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13718210
This i got from one of sites out there
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 600 total points
ID: 13718338
Here is the sample

In Declaration
<body>
    <%!
    Date theDate = new Date();
    %>
Hello!  The time is now <%= theDate %>

  </body>

Every time you refresh you will get the same date that is loaded first time.Keep the same code in scriptlet like this
<body>
    <%
    Date theDate = new Date();
    %>
Hello!  The time is now <%= theDate %>

  </body>

The date keeps changing according to the current date because service method is called everytime you reload.
0
 

Author Comment

by:lilyyan
ID: 13718617
if i run the above Declaration in different pcs at different time , i guess the time will be different and it will always keep same in the same pc.

or it doesn't matter if i use different pcs. as soon as , the jsp page is loaded at the first time, the time will never change
if the variable is declared in a Declaration <%! %>
0
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 600 total points
ID: 13718696
>it doesn't matter if i use different pcs. as soon as , the jsp page is loaded at the first time, the time will never change
  In what ever PC you run if you use declarations it will take the first loaded time for the JSP and it does not change.but run the same with scriptlet it will keep changing according to your PC time.
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13718702
Hope everything is clear now
0
 

Author Comment

by:lilyyan
ID: 13718793
Hello, very appreicate your all replies. They are very very helpful ! point increased . :)

0
 
LVL 8

Expert Comment

by:koppcha
ID: 13719222
Thanks for the grade
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13719463
Good luck! :-)

Tim
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this step by step method when setting up QuickBooks Online. They will allow you to explore the various features of the advanced settings available to you.
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question