Eliminate Scriptlet condtions in JSP

I have a Servlet that checks for information and if there is an issue it forwards the message to presentation page (JSP). Now I want to stop using conditions in scriptlets in the JSP. Please advise how I can do it in this situation in my Tomcat 4.1.27 container:
Servlet that forwards to JSP:
String gotopage = "";
if(mydata == 1)
     gotopage = /"pager.jsp?mymessage=err";
else if(mydata == 34
   gotopage = /"pager.jsp?mymessage=duper";
else
    gotopage = /"pager.jsp?mymessage=proc";
RequestDispatcher dispatcher =
  getServletContext().getRequestDispatcher(gotopage);     
dispatcher.forward(request, response);
...JSP
<%
String mymessage = request.getParameter("mymessage")
if(mymessage.equals("err"))
     out.println("Error on the page");
else if(mymessage.equals("dup"))
     out.println("Duplicate issue.");
else if(mymessage.equals("proc"))
     out.println("Process message issue");
%>I was thinking maybe a bean or regular Java class to handle this but not sure how. Here would be my method in a Java class:
public void getMessage(String msg)
     if(msg.equals("err"))
         out.println("Error on the page");
}Then I would put the method in a bean or what in JSP?
The Servlet would stay the same?

Put it in a bean or learn JSTL.
OR you could just put the message you want to display into page scope and have the JSP display it. Since the servlet already knows which page it's sending, let it figure out what the message should be, too.
no need for all that logic in the page.
do learn JSTL. Scriptlet code is very bad for maintenance.
%

Similar Messages

  • What version of Java can be used in the scriptlet part of JSP 2.1?

    hi
    can anyone tell me What version of Java can be used in the scriptlet part of JSP 2.1?
    Does that depend on the app server we use?
    we are not sure whether we can use in Java 5 in JSP 2.0 itself?

    yes im sure , im using thread in the both sidesthat doesn't give a deadlock... you need two or more threads running in the same side to get a deadlock! do you know what causes a deadlock?
    and all the streams and the fkuses are finehow have you tested that?
    about the broswe display i dont know i havent tried.http://forum.java.sun.com/thread.jsp?forum=31&thread=163929

  • Scriptlet within a jsp:include tag

    In Jrun 2.3.3 you could include a scriptlet within a <jsp:include> tag like this:
    <%
    String language = "E";
    %>
    <jsp:include page="/Conditions<%= language %>.html"/>
    Since I have to programme for four languages on our site, I used this convenient way of including different language pages on the fly all over the site.
    I then had a problem with 2.3.3 so I got 2.3.8 and discovered that all of these jsp:includes with scriptlets didn't work any more.
    I hastily uninstalled 2.3.8 and have continued with 2.3.3 ever since, thinking that they would sort this out in 3.0., which they didn't.
    Presumably to deal with this when I want to upgrade JRun I will have to use a load of if{} statements instead, but for four languages I will need four if {} statements which is incredibly long-winded compared to the single line of code above.
    <%
    if(language.equals("E")){
    %>
    <jsp:include page="/ConditionsE.html"/>
    <%
    %>
    Can anybody help:
    1) by giving me a neater solution that the one I've come up with.
    2) by explaining what happened at JRun to cause this change.
    Thanks.

    I could be wrong, but at least with WSAD (WebSphere), I couldn't do this:
    <html:text maxlength="14" size="10" property="proofOfClaim[<%=memberAccountIndex%>].sentDate%>"/>
    but could do this:
    <% String property = "proofOfClaim[" + memberAccountIndex +"].sentDate"; %>
    <html:text maxlength="14" size="10" property="<%=property%>%>"/>
    I think the reason is that since <html:text> is a custom tag, any scriptlets <%=...%> must be the ENTIRE value, and not be inserted in the middle of a value, for an attribute. Try it out. :)

  • Eliminate Scriptlets in form radio

    I have a web form working great for the past year using Scriptlets and Bean in Tomcat 6.0.20.
    Now I would like to eliminate the Scriptlets and use EL instead.
    Please advise how I can get the below radio input to work with EL in my JSP. All my attempts are not working.
    Bean that worked with the Scriptlet:
    public String getCityRadio(String myField)
          String myRadio = "";
          if(getCity() != null)
               if(getCity().equals(myField))
                     myRadio = "checked";
          return myRadio;
    }JSP works with Bean and Scriptlets:<jsp:useBean id="cityBean" class="beanPackage.CityBean" scope="request"  />
    Boston <input type="radio" name="city" value="Boston" <%= cityBean.getCityRadio("Boston") %>>
    <br>
    Buffalo <input type="radio" name="city" value="Buffalo" <%= cityBean.getCityRadio("Buffalo") %>>
    <br>
    Portland <input type="radio" name="city" value="Portland" <%= cityBean.getCityRadio("Portland") %>>My attempt to use EL instead of Scriptlet does not work with any of the below attempts. None of my EL attempts show any checked value or the check shows up only at the last radio button only.
    //tried this:
    Buffalo <input type="radio" name="city" value="Buffalo" ${cityBean.cityRadio["Buffalo"]}>>
    Portland <input type="radio" name="city" value="Portland" ${cityBean.cityRadio["Portland"]}>>
    //tried this:
    Portland <input type="radio" name="city" value="Portland" ${cityBean.cityRadio[city]}>>
    //tried this:
    Portland <input type="radio" name="city" value="Portland" ${cityRadio[cityBean.city]}>>
    //tried this:
    Portland <input type="radio" name="city" value="Portland" ${getCityRadio[cityBean.city]}>>

    You have in fact discovered one of the cases where there is no direct mapping between java code and EL : Calling a method that takes parameters.
    There is no way to do this in EL. You can't directly call methods that don't follow the getter/setter pattern.
    There are a few possible ways around this
    1: Put the condiitional logic onto the JSP page:
    Boston <input type="radio" name="city" value="Boston" <c:if test="${cityBean.city=='Boston'"}checked</c:if>>
    <br>
    Buffalo <input type="radio" name="city" value="Buffalo" <c:if test="${cityBean.city=='Buffalo'"}checked</c:if>>
    <br>
    Portland <input type="radio" name="city" value="Portland" <c:if test="${cityBean.city=='Portland'"}checked</c:if>>Hey, its view logic in any case. Why not?
    This is in fact a prime case for a custom tag.
    Particularly since you are referencing each value 3 times in each line of code here.
    A better approach would be to have
    - a list of values {Boston, Buffalo, Portland} - in an attribute called "cityList"
    - The currently selected value (eg Buffalo) (currently in cityBean.city)
    and then loop over your entire list, checking it against the currently selected value.
    <jsp:useBean id="cityBean" class="beanPackage.CityBean" scope="request"  />
    <c:forEach var="city" items="${cityList}">
       ${city}<input type="radio" name="city" value="${city}" <c:if test="${cityBean.city==city"}checked</c:if>>
    </c:forEach>cheers,
    evnafets

  • Get context path in a jsp without using a scriptlet

    Hi Guys
    I am using Scriptlet to get a application context path in a JSP. I want to get the context path with out using an scriptlet in a JSP, is there way to achieve this..?
    <%=request.getContextPath() %>/listofvalues.do?method=viewListOfValues thanks in advance.
    Regards
    Praveen
    Edited by: praveen_kumarvr on Jul 3, 2008 8:30 AM

    ${pageContext.request.contextPath}A common practice is to put this value in the <base href> in the HTML head and then use this as the root for all of the relative URL's in the page.

  • Can JSTL tags be used within scriptlet code?

    This may be a very basic question, I have only just started experimenting with JSTL in JSPs.
    I am trying to let the user save the content of the OutputStream to a file, and that works with the following code:
    <%
    String file_out = request.getParameter("file_content");
    out.write(file_out);
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition","attachment;filename=�ble�en.txt"); %>Both Firefox and IE display a dialog that lets the user save as a file, and I can use characters from any character set as the file name (JSPs are all in UTF8).
    I would like to pull the suggested file name from a resource bundle instead of fhardcoding it, however, so that I can display an appropriate suggestion for various languages. I can't figure out how to use a JSTL formatting tag for the file name with the scriptlet code, however, without getting a compilation error:
    <fmt:message key="file_save_as" />
    I can display localized messages just fine, as long as I don't try to do it within scriptlet code. I have tried to split the scriptlet code above, with the JSTL tag in between, but I can't find a way that works. Any pointers would be appreciated.

    am trying to let the user save the content of the OutputStream to a fileThis is something better done in a servlet than a JSP.
    A jsp can add extra carriage returns into your output stream, which might corrupt your data.
    I can't figure out how to use a JSTL formatting tag No, you can't mix scriptlet code and tag code.
    The JSTL tags are written for JSPs to eliminate scriptlet code from the page. If you are doing this in scriptlet code anyway, I suggest you use the ResourceBundle classes as you normally would in java code. Thats what the fmt:message tag is doing behind the scenes anyway.
    ResourceBundle bundle = ResourceBundle.getBundle("myApp.properties");
    String filename = bundle.getString("file_save_as");Cheers,
    evnafets

  • EL or RT expressions for JSP taglibs

    I have a simple question
    We all know that there are two ways to implement a JSP page. either RT expressions or expression language (EL). I have done plenty of research and I am comfortable at creating custom JSP taglibs that support both RT and EL. What I want to know is which if them is used more commonly?
    Its useless to right 2 seperated libs, since you can pass variables from EL to RT and vice versa, so I want to have only one tag library, but I dont know how I should implement it EL or RT.
    What is used more often and why? What is the advantages of using one over the other. Thank you!!!

    RT was the original way; it required scriptlets, which everyone hates.
    EL is the new way; it helps eliminate scriptlets, which everyone loves.
    EL is the method of choice; even moreso when JSP 2.0 is finalized, it allows them to be used more freely (not just in tags)

  • A application+designpattern with JSP

    Hello!
    I have a question regarding designpattern for an application using JSP.
    Which is prefered
    1) Use a bean that have all methods the application is using
    2) A couple of beans that have different methods
    3) to have the basic Java in the JSP page (databaseconnection etc.)
    Is it prefered that always use beans to minimize the JAVA in the JSPpage or?
    Thanks in advance // DS

    Hello!
    I have a question regarding designpattern for an
    application using JSP.
    Which is prefered
    1) Use a bean that have all methods the application is
    using
    2) A couple of beans that have different methods
    3) to have the basic Java in the JSP page
    (databaseconnection etc.)I would prefer 2. Have methods that are associated with each other in one class, but unassociated methods in another... for instance:
    Have a DataBase access bean that handles connections to database
    Hava a another one to represent a User who is logged in, that holds all associated methods for the user.
    Have another that would represent an item you have in your inventory, which would have the associated methods for that...
    etc... obviously what classes you use will be based on what your application is meant to do. But if you take every 'real world' thing that is associated with your app, then make a class specifically for that thing, that is the method I prefer.
    >
    Is it prefered that always use beans to minimize the
    JAVA in the JSPpage or?Yes, do this. It makes it a lot easier to debug, and to move from one system to the next. For instance, if you put all your logic into beans, then you can test and retest in a normal java environment, like the command line. Then you just make the JSPs that display the data after your Java code is tested, and you can be sure that any problems are associated with the JSP display....
    Look into JSTL or Struts as a means to eliminate just about all scriptlets from the JSP.
    >
    Thanks in advance // DS

  • Remote system JSP project not working in eclipse

    Hi
    I am developing a JSP project. My files are on a remote server and I want to configure it in Eclipse 3.3 with Lomboz. The problem is After adding the project it gives a warning that xml files can't be validated.The exact warning is
    The file cannot be validated as the XML Schema "\192.168.1.10\gunjan_share\workspace\Project1\WebContent\WEB-INF\web.xml (The system cannot find the path specified)" that is specified as describing the syntax of the file cannot be located.
    Due to this error JSP pages are not getting compiled.
    Plz help me
    Thanks

    Extracted from Note 1067696.1:
    You need to either adapt your code to remove the scriptlets in your JSP page,
    or you can use the following Java option to disable OJSP:
    -Dadfvdt.disableOjspDeployment=true
    You can add this option -Dadfvdt.disableOjspDeployment=true in your file "ide.conf" in the directory "<Middleware_Home>\jdeveloper\ide\bin"
    For your Production environment, you can have your Managed WLS ignoring the OJSP mode by adding the Java Option -Dadfvdt.disableOjspDeployment=true
    * in the "startManagedWebLogic" (if you want to limit it to a specific Managed Server)
    * or "setDomainEnv" (for all managed servers in a domain).

  • Passing values from a jsp to a servlet

    I have a jsp which has a search form for customers, first it lets you search by colour, giving options of red, white or all, then gives three options of what price. Im trying to get my servlet to read these choices, and display the results. I have a statement in my serlvet which says:
    String price = request.getParameter("search preference colour");
    and the statment in the JSP is:
    <select name="search preference colour">
    <option value="Red">red</option>
    <option value="White">White</option>
    <option value="All">All</option>
    </select>
    I need to know how to define which option has been chosen, and have the chosen results displayed. I tried an 'if' statement but it didnt seem to work. Thanks

    yuvi wrote:
    i have a servlet from which i am passing result set values to a jsp page.Basically they are database records from a table named basic.
    and these selective fields have to be dissplayed on jsp.i have tried incorporating values into session variables and using them on jsp.it works fine for single record but when there are multiple results it fails!! :(Learn JSTL and use tags. Scriptlet code in JSPs is a bad idea.
    %

  • Jsp sriplets not working in properly in ojsp

    Hi every one i have this ADF application develped in jdevelper 11.1.1.0.2 and deployed in to weblogic server version 10.3.0 and it worked fine.plese note i have used
    ADF for the presentaion layer.We had to migrate in to weblogic server version 10.3.2.for this i used jdevelper version 11.1.1.2.0 and open the project from this and i click yes for the migration.
    then i have deployed the new war file in to the new server.but the thing is it doesnt work the expected way.for my knowldge the issue is something to do with ojsp.when it tries to read jsp scriplets in the jsp page it throws an error.it is as followes
    **JspServlet error: Servlet unable to dispatch to the following requested page: The following exception occurred:oracle.jsp.parse.JavaCodeException:  Line # 13, oracle.jsp.parse.JspParseTagScriptlet@3f673160***
    **Error: Java code in jsp source files is not allowed in ojsp.next mode.>**
    any solution to this.????can i disable the ojsp compiler and go to normal jsp compliser?if so how to do it?
    thanks in advance every one :)
    Umesh

    Extracted from Note 1067696.1:
    You need to either adapt your code to remove the scriptlets in your JSP page,
    or you can use the following Java option to disable OJSP:
    -Dadfvdt.disableOjspDeployment=true
    You can add this option -Dadfvdt.disableOjspDeployment=true in your file "ide.conf" in the directory "<Middleware_Home>\jdeveloper\ide\bin"
    For your Production environment, you can have your Managed WLS ignoring the OJSP mode by adding the Java Option -Dadfvdt.disableOjspDeployment=true
    * in the "startManagedWebLogic" (if you want to limit it to a specific Managed Server)
    * or "setDomainEnv" (for all managed servers in a domain).

  • Writing java code in JSP

    Hi All
    I dont know which one of the following is more effective in JSP
    writing java code like <% out.println("Hellooo "); %>
    OR
    writing like <%="hello" %>
    I shall be very much thankful to all those who spare their valuable time to clarify my doubt. I hope my question is not a stupid one.
    Thanks
    Elisha

    when both gives the same out put, why there are two for the same out put?Because it is easier to do this:
    Hello <%= userName %>
    than this:
    Hello <% out.println("userName"); %>
    Also there is a BIG difference in readability and maintainability of the JSP page. JSP pages are all about having html, and a few "fill in the blank" spots. The less <% scriptlet code %> you put on your JSP page, the better. If you want to run java code, do it in a servlet/bean.
    With the advent of JSTL, scriptlet code in JSPs should be a thing of the past.

  • Why do we need jsp:useBean???

    Hi All,
    I am going over both the J2EE tutorial and JSP1.2 spec.
    As far as I read, it seems to me that "jsp:useBean" is a way of creating a bean object.
    <jsp:useBean id="local" scope="application" class="Mylocales">
    If I put the class file in the import statement, can I create the object in the scriptlet
    <% Mylocales local = new Mylocales(); %>
    and set/get properties by calling
    local.setName(); // assume these methods are in the class
    local.getName();
    without using "jsp:setProperty" and "jsp:getProperty".In this way, isn't it a redundancy to have "jsp:useBean" in JSP spec? We can always create any kind of objects in the scriptlets.
    Since "jsp:useBean" has been there for years, I believe something must be wrong with my rationale. However, both the spec and the tutorial couldn't answer this question. Can anyone clear it up to me?
    Thx in advance.
    Kevin

    There's nothing wrong with your rationale; useBean IS a redundant tag, but it does provide some compact functionality without having to write extra code.
    http://java.sun.com/products/jsp/tags/11/syntaxref1115.html covers the syntax of this tag; one advantage of using this tag is the 'scope' attribute, that allows you to define, at object creation, just how long the object will be around. You can limit an object's scope to the page that declared it, or allow it to be accessed by any page that shares the same session, or request, or application.
    I do believe that the main point of this tag comes in at the design level; one of the strongest reasons to use JSPs at all is to separate out the application logic from the page presentation. This means that anyone who can use HTML should be able to maintain the JSP pages in the application. Instead of having to teach a non-programmer a little bit of code, just demonstrate the use of a single tag that can be reused throughout the application. The logical extension of this idea is the tag library.
    Myself, I mix and match as needed, but there are never more than a few lines of scriptlet code in my JSP pages. If there's more code than HTML in my JSP page, I'll go ahead and use a servlet instead.
    Anyway, I guess my point is that JSP pages are flexible, you can pick the functionality that you want to use, and the useBean tag isn't quite superfluous.

  • How to access dataprovider through jsp syntax

    I am creating an image gallery but here's my issue...
    I have a database table that contains links to images on my file system. I created a dataprovider for this database table on my page so the dataprovider now returns all the image links.
    Now what I need to do is create a row of thumbnails so I add a scriptlet in my jsp code where I want the thumbnail to appear. This scriptlet loops through the dataprovider and for each row it will create a standard html image tag populating the src attribute with the link from the dataprovider.
    I figure creating dynamic html img tags is much easier than creating dynamic image components in the backing bean. Doing the former allows me to output the row of thumbnails exactly where I want them on the page (ie where i put my scriptlet code) and is easier to manage.
    The problem is i don't know how to access the dataprovider through jsp tags and syntax. I'm sure there must be a way, can anyone help?
    Thanks.

    I've done this sometimes using scriplets:
    <%
        request.setAttribute("SOME_CONST", Constants.SOME_CONST);
    %>
    <c:out value="${SOME_CONST}" />But I would also be interested if anyone knows a way without those ugly scriplet..
    O

  • Display more than one BI bean graph on JSP page

    Hi,
    I want to show BI Beans graphs on a JSP page (just the Jdev 10.1.2 included components, not the OLAP backed ones) but I want to show a different graph for each row iterated in the JSP so I'll end up with up to 20 graphs on the screen.
    Currently I can have it showing the 20 graphs quite happily, but they're all the same (that is, they all look the same but they have different IDs). I note from the way I understand this works that they're all being made off the currentRow of the viewobject. I want to somehow increment the current row for the data as the view iterator advances. I could break the model 2 separation and do it with java scriptlet in the JSP but this is my least favoured way. Is there some other way I can have all the graphs prepared from a struts action which does advance through the data and then goes on to show the jsp?
    I'm guessing this is somewhat related to a master detail graph - except when I tried to run with one I got some nasty exceptions within the graph tag library. I'm not sure that's what I want anyway as I need to format the page as a report so I need each graph to be a separate image.
    Any help appreciated
    - Nathaniel

    What I'm after is a way to create the BI Beans graphs supplied with Jdev 10.1.2 ni a java class rather than a JSP. Is there a way to do this?

Maybe you are looking for

  • Sales Order - Source List Issue ( Help needed)

    Hi All... The user is trying to create a Sales order , but could not do it for a particular Customer and we are getting a error * Source not included in list despite Source List Requirement" and then we are getting an error Incorrect Index Structure

  • Updates failed on Adobe Creative Suite 5 Web Premium

    Hello, I have a problem. My hard disk crashed so I had to replace it and reinstall my Adobe Creative Suite 5 Web Premium. The installation was fine, I used of course the Serial Number on the box, but when I tried to launch the update I got the messag

  • Syncing Mail between two computers

    I use both a desktop and a laptop computer and need to sync my work files including my >10GB Mail folder between the two. For the most part I have been able to do this pretty successfully using Chronosync. I have a firewire drive that I plug in and s

  • My mac wont power up

    My emac has suddenly stopped working. It worked fine one day and then nada. I have tried different power cables from other emacs with different sockets and still no joy. It is a 1.25ghz emac and I am puzzled as to why it has just stopped. Does anyone

  • Query on SAP IS Banking - SAP Deposits Management/ SAP BCA in ECC 6.0

    Dear all, In SAP Deposits Management in the new platform, we have the node Financial Services both in Easy Access and IMG under which we have the major functionalities like Account and Product Management. However this is unavailable in ECC 6.0. Reque