Solved

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

Posted on 2002-04-25
9
345 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
A Short Story about the Best File Recovery Software – Acronis True Image 2017
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now