Writing functions in jsp

I want to write functions which can take some parameter and returns an array in jsp

Hi Here is the code
Include this file where ever necessary
pass the parameter u 'll get the result
<%!     
public String[] cre(int n){
String str[]=     {"asdf","asdf","vac"};
switch(n)
case 1:
str[0]="It is 1";
break;
case 2:
str[0]="It is 1";
break;
default:
str[0]="It is 1";
str[0]="Invalid parameter Passed ";
return str;
%>
Bye answer whether satisfied or not

Similar Messages

  • How to run the recursive function in jsp

    hi, can any body tell me how to run
    rrays.sort(files);
    for (int i = 0, n = files.length; i < n; i++) {
    for (int indent = 0; indent < indentLevel; indent++) {
    System.out.print(" ");
    if (files.toString().endsWith(".jsp"))
    System.out.println(files[i].toString());
    if (files[i].isDirectory()) {
    listPath(files[i]);
    indentLevel--;
    this code in jsp
    thanks in advance

    Why are you writing scriptlets and functions in JSPs?
    Functions are supposed to be written in the Java Application layer.
    JSPs are best suited only to display information.
    Also why are you using Scriptlets insted of JSTL ?
    Anyway, if you want to write in JSPs only , here's how to write a function in them: http://forum.java.sun.com/thread.jspa?threadID=496577&messageID=2341986 you can figure out the rest.

  • Writing functions that contain scriptlets/expressions

    How do I go about writing functions in my JSP page that contain scriptlets and expressions? Also, I'd like to reference session/request/response variables from within.
    I want something like this:
    <jsp:useBean id="myObject" class="com.classes.*" scope="session" />
    <%!
    void printList()
         String str1 = myObject.getName();    <%-- reference outside object --%>
         String str2 = request.getParameter("address");  <%-- use request variable --%>
         out.println(str1 + ": " + str2); <%-- I know it's illegal to do this, but how do I print to the browser from a function? --%>
         <%= request.getParameter("zipcode") %> <%-- Also illegal (are expressions allowed?) --%>
         out.println("<A HREF=\"<% session.getAttribute("url") %>\">Click here</a>");  <%-- dynamic and static content: how do I do this? --%>
    %>I unsure as to how to carry out the above actions. My biggest problem is the last example. How do I print out HTML interspliced with dynamic content with my function?
    I'm pretty sure that nesting <% %> tags inside of <%! %> directive tags is illegal. Is writing a void function that prints out a bunch of static and dynamic data simply not possible with JSP? If so, is it necessary that I write several functions to return values which can then be printed later in the page via separate scriptlets or expressions?

    Take a look at generated servlet code and try to understand how it is working.
    request and response is available only inside some method body. So to access it in your method, you must pass it as argument.
    void printList(HttpServletRequest request, HttpServletResponse response) { ... }everything inside
    <%!
    is added to servelt code, but things inside <% %> are added to processing method body not servlet body.
    Take look at jsp and corresponding servlet:
    JSP:
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <html>
    <head><title>JSP Page</title></head>
    <body>
        <%="place 1"%>
    </body>
    </html>
    <%!
        public String aaa() {
            return "palce2";
    %>GENERATED SERVLET
    package org.apache.jsp;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
        public String aaa() {
            return "palce2";
      private static java.util.Vector _jspx_dependants;
      public java.util.List getDependants() {
        return _jspx_dependants;
      public void _jspService(HttpServletRequest request, HttpServletResponse response)
            throws java.io.IOException, ServletException {
        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        JspWriter _jspx_out = null;
        PageContext _jspx_page_context = null;
        try {
          _jspxFactory = JspFactory.getDefaultFactory();
          response.setContentType("text/html;charset=UTF-8");
          pageContext = _jspxFactory.getPageContext(this, request, response,
                         null, true, 8192, true);
          _jspx_page_context = pageContext;
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;
          out.write("\n");
          out.write("\n");
          out.write("<html>\n");
          out.write("<head><title>JSP Page</title></head>\n");
          out.write("<body>\n");
          out.write("    ");
          out.print("place 1");
          out.write("\n");
          out.write("</body>\n");
          out.write("</html>\n");
          out.write("\n");
        } catch (Throwable t) {
          if (!(t instanceof SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              out.clearBuffer();
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        } finally {
          if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
    }

  • How to call java script function from JSP ?

    how to call java script function from JSP ?

    i have function created by java script lets say x and i want to call this function from jsp scriplet tag which is at the same page ..thanks

  • Access session variable in Java Function in JSP

    Hi Experts !!
    I am developing an application using STRUTS MVC...
    Very sorry if u have problem understanding my question, i ll try to improve... and sorry if i can't post codes here
    Basic question is ....
    I want to open a word document on pageLoad of JSP, the word document is not a single document, there is a form in which there is a "name" and "template" whenever user clicks on a button there is one action attached to it which creates a copy of that template in a different folder.. on the next page OnLoad i want to open that particular document. for that i have created a variable and also have set in the session, just want to access it in the below code.
    I have a formbean in which i have a variable, the scope is session, that variable i have put it in session also. but i want to access that variable in a "java function" in JSP so that "onLoad" page that function should work.
    JSP---
    <script type="text/javascript">
    function openDocument() {
    var w = new ActiveXObject("Word.Application");
    var docText;
    var obj;
    var a;
    if (w != null) {
    w.Visible = true;
    obj = w.Documents.Open(I HAVE TO ACCESS THAT VARIABLE HERE);
    </script>
    FORMBEAN----
    public class CreateSOWFormBean extends ActionForm {
    private String workflowName;
    private String comment;
    private String sowTemplate;
    private String sowFileCreated;
    public String getSowFileCreated() {
    return sowFileCreated;
    public void setSowFileCreated(String sowFileCreated) {
    this.sowFileCreated = sowFileCreated;
    sowFileCreated is the variable that i have accessed in session and that value i have to pass in that function in JSP....
    I am aware of something like
    obj = w.Documents.Open(<%'sowFileCreated'%>);
    but i m not sure how to write....
    Plz help.....

    If you're working under a framework like struts you should definitely be using JSTL tags rather than scriptlet code to access variables within the page. With JSTL code <% codes can be almost entirely avoided.
    To transfer a value from a Servlet to a JSP don't use a session variable, use a request attribute. Session variables should only be used when values have to survive from one transaction to another.
    You can write something like:
    obj = w.Documents.open('<c:out value="${openURL}"/>');in the Javascript portion of your JSP.
    Just beware of potential problems with quotes. The coresponding code in the Servlet would be like:
    request.setAttribute("openURL", openUrl);

  • How to realize print function in JSP

    How to realize print function in JSP? I know use Javascript:window.print() can print,but it will print whole page,it don't fit me;Another method is to write JSP into Excel and then print it in Excel,but it don't fit me too,because it don't print directly. Anybody know how to print it directly?

    Many websites offer a "printer friendly" version of a certain page. That IMO is the only way to solve your problem, next to using CSS to use a different style for printer output. Here is a tutorial that covers this:
    http://www.alistapart.com/stories/goingtoprint/

  • How to write printing function using JSP?

    dear all,
    i want to add a printing function in my project but i do not know how to do it? could anyone teach me how to do it?
    thanks
    eric

    salut eric,
    you can do it this way :
    1 - create a html link in your code to this javascript
    function : (I am using Struts, but you can write it in
    pure html
    <html:link href="#bodyStart"
    onclick="openWindowForPrinting()"
    titleKey="msg.status.print">      <bean:message
    key='app.print' />
    <script language="JavaScript">
    <!--
    window.open = SymRealWinOpen;
    //-->
    </script>
    </html:link>
    >
    >
    2 - here is the javascript function : it open a window
    with the "print.jsp" page.
    function openWindowForPrinting(){
    window.open('/your_project/pages/common/print.jsp','',
    'left=150, top=150 , status=no, directories=no,
    toolbar=no, menubar=yes, location=no, scrollbars=yes,
    resizable=yes, dependant=yes');
    3 : here is the print.jsp page : You have only to
    create a session variable called
    "currentBodyRelativeLocation", which content the
    relative URL to the page you want to print (for
    example, the boby of the current page).
    <%@ taglib uri='/WEB-INF/tlds/perso.tld'
    prefix='perso' %>
    <html>
    <head>
    <perso:csslink page='${cssRelativeLocation}' />
    </head>
    <body onload="var SymTmpWinOpen = window.open; window.open = SymWinOpen; printPage();; window.open = SymTmpWinOpen;">
    <body>
    <jsp:include page="${currentBodyRelativeLocation}"/>
    </body>
    <script language="JavaScript">
    <!--
    window.open = SymRealWinOpen;
    //-->
    </script>
    </html>
    <script language='javascript'>
    function printPage(){
    window.print();
    window.close();
    </script>
    hai,
    i'm not very understand how is your code work. when i write html code, do i need to mind about this code: "msg.status.print" ? what is this code mean?
    i have too much question on this thing. would you mind to be further explain to me? or is it any reference for me to refer? i'm really new to write printing function on JSP.
    i never write this function before. i'm feel so lost.....:(
    thanks again
    eric

  • Passing values to JavaBean function using jsp scriplet

    Hi,
    I have a JavaBean funtion that is SaveData(parameters)
    which save the values from HTML form.
    Now i want a code for pass the values from HTML form to SaveData() function using jsp scriptlet.
    I used the code
    <jsp:useBean id="JDBC" class="bean.JDBCBean" scope="application"></jsp:useBean>
    <%=JDBC.saveData(phonenumber,manufacturer,model,email,country,newsletter)%>
    Here
    JDBCBean -> Bean Name
    phonenumber,manufacturer,model,email,country,newsletter ->HTML form's text values
    But it didnt work properly.
    Anyone help me to correct this.

    Hi,
    i have the same code like above
    I have to pass the parameters with form name under the following code.
    <jsp:useBean id="JDBC" class="bean.JDBCBean" scope="application"></jsp:useBean>
    <%=JDBC.saveData(registration.phonenumber.value,id_type.value,id_style.value,registration.email.value,registration.scountry.value,player_type.values)%>
    Here
    registration -> HTML form name
    But i got the error for
    package registration does not exist
    out.print(JDBC.saveData(registration.phonenumber.value,id_type.value,id_style.value,registration.email.value,registration.scountry.value,player_type.values));
    How to i correct this?
    Anyone help me......

  • Equivalent function of JSP:include or @include?

    Could someone please let me know how I can include html pages in a java servlet page? I am looking for similar function as JSP include. A simple example would be must appreciated. Thanks!

    Thanks for you reply. Actually, what I want to do is to import/include a static html page to my java servlet page. I do not want to use JSP because all the pages created were sevlet. I found out a way that using getServletcontext.getResource to read in the html page and print it out from the servlet. Just wondering that if there is a better way to do this?

  • Exit a function in JSP

    How can I exit a function in JSp
    my code is
    <%! String Dothis(int level)
    if(level==2)
    //exit the function
    else
    %>
    How can I exit this function .....
    Break label does not work...as I am not able to declare a label
    Pl...guide me
    smita

    Looking into ur code ,,,, U can do it like the following in addition to solutions posted above ( Exception and return)
    <%! String Dothis(int level)
    if(level != 2)
    %>
    Thanks
    Sampath ...

  • Same as PHP Functionality In JSP, HOw?

    Hi,
    I am a developer having experiance in PHP with Oracle, I recently have projects to develop in JSP, I want know does JSP also have the Variable in Variable functioanlity in as it's available in PHP?
    In php I stored my variable name in a PHP variable as
    $Asif='My Name is Asif';
    $a='Asif';
    Echo "Here we can get the result of variable Asif using variable a: ".$$a;
    in the above by using $$ it understand the inside value is the actual variable name whose value is to print..
    I need same functionality in JSP.....
    if Any one can help...
    THanks in Advance.
    Asif

    The same rule applies for textboxes as for checkboxes with the same name - indeed for any components accessed in the dom via javascript.
    If there is only one element of that name, it is accessed directly.
    If there is more than one element of that name, it reveals them as an array.
    so document.testForm.a_date would return an array of input fiels
    document.testForm.a_date[0], document.testForm.a_date[1] ... document.testForm.a_date[n]
    However the order of submission of parameters is NOT guarunteed, which is why people normally DON'T have fields in this manner - because you can't know for certain that a_date[1] matches with b_date[1] at the server end.
    The common practice is to give each field a unique name (numbered).

  • EMAIL FUNCTION USING JSP

    HOW CAN I WRITE A EMAIL FUNCTION USING JSP
    WHICH HAVE FORWARD, REPLY, REPLAY ALL, AND ADDRESS BOOK LIKE YAHOO EMAIL FUNCTION
    HOW CAN I WRITE IT?

    I meant you would face a considerable challenge trying to write
    public static void main(String[] arg) { } with your Caps-Lock key stuck.
    You would not even be able to escape your way out of it with
    \u0070\u0075\u0062\u006C\u0069\u0063 \u0073\u0074\u0061\u0074\u0069\u0063 \u0076\u006F\u0069\u0064 \u006D\u0061\u0069\u006E(S\u0074\u0072\u0069\u006E\u0067[] \u0061\u0072\u0067) { } as the "\u" escape sequence also contains a lowercase character.

  • How to create download function in JSP?

    Can I know how to make a download function in JSP? Any sample?. Thank You very much!

    http://forum.java.sun.com/thread.jspa?threadID=5312541&tstart=0
    Sorry but whining about it in duplicate threads doesn't help.
    HELP DENIED!

  • Writing Function Modules

    Hi
    I want to learn how to write function modules in ABAP please guide me  how should I proceed to learn it as fast as I can.
    Are there any Demos or PDF documents for that.
    I have gone through ABAPDocu  but it has a lot of unwanted materials as well and I found it a bit unstructured to learn writing function modules.

    Hi,
    Please chek this link
    Function Groups
    http://help.sap.com/saphelp_nw70/helpdata/en/9f/db992335c111d1829f0000e829fbfe/content.htm
    Creating Function Modules
    http://help.sap.com/saphelp_nw70/helpdata/en/9f/db98fc35c111d1829f0000e829fbfe/content.htm
    Best regards,
    Brijesh

  • Need help in writing data from JSP to excel

    Hi ,
    I need help in writing the data from JSP to excel.I somehow able to retrieve the data into excel but unable to get the required format.
    For eg: The amount should be displayed in 0.00 format .when i am exporting it to excel it is displaying as 0 :( .
    I am using the following code in JSP.
    "out.print(amt + '\t');"
    Would like to know if there is any otherway where in i can get my requirement.
    Thanks
    Tom

    Hi,
    Try using format part of the JSTL tag libs.
    Syntax :
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <fmt:formatNumber value="40" pattern="$#,##0.00"/>
    I need help in writing the data from JSP to excel.I
    somehow able to retrieve the data into excelHow do u convert the jsp to excel?
    One way to convert the jsp page to excel, is to render it as an excel appl instead of html. Set the content type of the response to application/ms-excel.
    response.setContentType("application/ms-excel")Hope this Helps....

Maybe you are looking for

  • URGENT Please help with weird behavior of test suite

    Hi All, I have a composite involving three operations of two different services. The weird behavior comes by the time on which I run the test suite on the EM, if I try to emulate two different operation response from one service, by the time that the

  • Where can i find remarks in oralce applications

    Hi, I have an old instance. 11.5.9. I need to find the remarks in it. Can any one help where to look plz. Regards

  • Illustrator CC doesn't work anymore

    Illustrator CC was working perfectly. Since last week it will no longer open and crashes before the splash screen opens. I re-installed it but it still don't work. I saw in other discussions that i'm not the only one.

  • Need a good guide on Refreshing a Database

    Hi guys.. i am looking for a good guide which can help me get thru the best practices for refreshing a database step by step. Regards Monu Koshy

  • Postgres update is not backwards compatible

    Hi all, the 8.4 postgresql server updated recently isn't backwards compatible, it refuses to read the old database files. Basically I have wasted 2 hours of my life because arch happily upgraded postgres without saying anything.  If I knew this befor