Solved

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

Posted on 2002-04-25
9
359 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Getting to know the threat landscape in which DDoS has evolved, and making the right choice to get ourselves geared up to defend against  DDoS attacks effectively. Get the necessary preparation works done and focus on Doing the First Things Right.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

622 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