Solved

How do I write a function in JSP and return a value ?

Posted on 2002-04-25
9
358 Views
Last Modified: 2006-11-17
Here is my code :

  Cookie[] cookies = request.getCookies();
  if (cookies != null){
    for(int i = 0; i < cookies.length; i++){
      if (cookies[i].getName().equals("Name")){
        return cookies[i].getValue();
        break;
      }
    }
  }

I would like to make a function out of it, so this is
what I have :

<%
  String getValue1(String name){
  Cookie[] cookies = request.getCookies();
  if (cookies != null){
    for(int i = 0; i < cookies.length; i++){
      if (cookies[i].getName().equals("Name")){
        return cookies[i].getValue();
        break;
      }
    }
  }
}


out.println(getValue1("Name"));
%>

and this is the error I got :

Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\examples\test2$jsp.java:55: ')' expected.
                  String getValue1(String name){
                                         ^
1 error


how to make it right ?
0
Comment
Question by:tanc02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6970244
<% code %>
Code is inserted in service method.


<%! code %>
Code is inserted in body of servlet class, outside of service method.

Do to make a method, you should define in <%! code %>

<%!
String getValue1(String name){
 Cookie[] cookies = request.getCookies();
 if (cookies != null){
   for(int i = 0; i < cookies.length; i++){
     if (cookies[i].getName().equals("Name")){
       return cookies[i].getValue();
       break;
     }
   }
 }
}
%>
<% out.println(getValue1("Name")); %>
0
 
LVL 1

Author Comment

by:tanc02
ID: 6971075
kotan,

I used your code, and this is what I got :

Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\examples\test2$jsp.java:13: Return required at end of java.lang.String getValue1(java.lang.String).
        String getValue1(String name){
               ^


An error occurred between lines: 1 and 13 in the jsp file: /test2.jsp

Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\examples\test2$jsp.java:14: Undefined variable or class name: request
        Cookie[] cookies = request.getCookies();
                           ^


An error occurred between lines: 1 and 13 in the jsp file: /test2.jsp

Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\examples\test2$jsp.java:19: Statement not reached.
              break;
              ^
3 errors
0
 
LVL 1

Expert Comment

by:iartmirko
ID: 6971089
Kotan is right...the code you showed will be integrated in the service method of the JSP-Servlet, so you have a method declaration in a Java method!

The best way would be to put the getValue1()-method in a bean and you use the bean in your JSP. So you don't have to redefine the method in every JSP you use.

Mirko
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:pellep
ID: 6971182
<!%
 String getValue1(String name, ServletRequest request){
 Cookie[] cookies = request.getCookies();
 if (cookies != null){
   for(int i = 0; i < cookies.length; i++){
     if (cookies[i].getName().equals(name)){
       return cookies[i].getValue();
       break;
     }
   }
 }
 return null; // cookie not found
}
%>

in you jsp

<%
out.println(getValue1("Name", request));
%>
0
 
LVL 1

Author Comment

by:tanc02
ID: 6971232
This is what i got now :

Generated servlet error:
D:\Program Files\Apache Tomcat 4.0\work\localhost\examples\test2$jsp.java:59: Method getValue1(java.lang.String, javax.servlet.http.HttpServletRequest) not found in class org.apache.jsp.test2$jsp.
                out.println(getValue1("Name", request));
                                     ^
1 error
0
 
LVL 4

Expert Comment

by:pellep
ID: 6971255
<!%
String getValue1(String name, HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if (cookies != null){
  for(int i = 0; i < cookies.length; i++){
    if (cookies[i].getName().equals(name)){
      return cookies[i].getValue();
      break;
    }
  }
}
return null; // cookie not found
}
%>

in you jsp

<%
out.println(getValue1("Name", request));
%>
0
 
LVL 1

Author Comment

by:tanc02
ID: 6972238
Still got the same error !
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6972785
I am not going to say this is going to work.. but try this:
(I don't have access to my dev server right now)

<!%
static String getValue1(String name, HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if (cookies != null){
 for(int i = 0; i < cookies.length; i++){
   if (cookies[i].getName().equalsIgnoreCase(name)){
     return cookies[i].getValue();
   }
 }
}
return null; // cookie not found
}
%>

<%=getValue1("Name", request)%>
0
 
LVL 6

Accepted Solution

by:
kotan earned 100 total points
ID: 6976141
<!%  --> <%!

Then, the compele one is

<%!
Public String getValue1(String name, HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if (cookies != null){
 for(int i = 0; i < cookies.length; i++){
   if (cookies[i].getName().equals(name)){
     return cookies[i].getValue();
     break;
   }
 }
}
return null; // cookie not found
}
%>

in you jsp

<%
out.println(getValue1("Name", request));
%>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery - parsing the results returned from webmethod 1 128
Fisheye tool 2 125
Reading data  from excel sheet and displaying 1 97
Spring MVC - sending raw charset to backend 3 271
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
We asked our MSP customer base what their favorite tools were and how they help them serve clients. We focused our questions on favorite tools in the following categories: >PSA tools >RMM tools >Alert management tools >Communication tools and Mo…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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