Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-04-25
9
Medium Priority
?
361 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
Industry Leaders: 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If you’re involved with your company’s wide area network (WAN), you’ve probably heard about SD-WANs. They’re the “boy wonder” of networking, ostensibly allowing companies to replace expensive MPLS lines with low-cost Internet access. But, are they …
On September 18, Experts Exchange launched the first installment of the Help Bell, a new feature for Premium Members, Team Accounts, and Qualified Experts. The Help Bell will serve as an additional tool to help teams increase question visibility.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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