Solved

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

Posted on 2002-04-25
9
355 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

This article describes how to reset your Windows 10 password when you've forgotten it.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

837 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