0

Edited, Still this is not working exactly, but I need to set a page_type on each page of a website. That page_type needs to be checked by some java and do code based on the page_type. The problem is, the if else statements are not being done correctly. Nothing happens because the parameter doesn't seem to be placed. main page where page_type is being set:

<div id="DYK_area">
 <jsp:include page="/imgs/random_dyk.jsp" flush="true">
 <jsp:param name="Page_type" value="prenatalOverviewArea"></jsp:param>
 </jsp:include>
</div>

Where the code is being checked

<% String pageType = request.getParameter("page_type")==null?"": request.getParameter("page_type"); 
String image_name="";   
    if(pageType.equals("prenatalOverviewArea")){
        int random = (int )(Math.random() * 10 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("prenatalNorm")){
        int random = (int )(Math.random() * 25 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("overviewArea")){
        int random = (int )(Math.random() * 20 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }
    else if(pageType.equals("overviewNorm")){
        int random = (int )(Math.random() * 45 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }

if(!image_name.equals("")){
%>

<img id="dyk_random" src="<%=image_name%>"/>

<%}%>
user2089255
  • 87
  • 2
  • 12
  • What's `Page_type`? `request.getParameter` reads the param but it doesn't automagically assign it to a variable. Use something like this instead: `String type = request.getParameter("Page_type");` and use the `type` variable in your conditions. – toniedzwiedz Sep 03 '13 at 16:44
  • I am trying to set the page_type to the page I am on so the if statement will see what code to run. I have 0 exp with java, so this is all new ={>) – user2089255 Sep 03 '13 at 16:46
  • 1
    Also, while using scriptlets like this, refrain from using the `==` operator while comparing `String` objects. Use the `equals` method instead. `==` or `eq` is alright in the JSP Expression Language but not in scriptlets. – toniedzwiedz Sep 03 '13 at 16:49
  • These are good tips, but nothing has changed in what is displayed. I know too little about java, i may need a working example of this. – user2089255 Sep 03 '13 at 16:53
  • 1
    Related: http://stackoverflow.com/q/5444083/1065197. By the way, stop using scriplets, their usage is highly discouraged, instead use EL and JSTL. More info: http://stackoverflow.com/q/3177733/1065197 – Luiggi Mendoza Sep 03 '13 at 16:54
  • Wow, this looks a bit confusing, maybe a bit above my pay grade ={>) – user2089255 Sep 03 '13 at 17:20
  • Try isolating the problem. First, change the name of the `` from `Page_Type` to `pageType`. Second, in `/imgs/random_dyk.jsp` page, remove all the scriplets and just add `${param.pageType}`. Clean and build your project and redeploy it, then add in comments the result displayed in the page. – Luiggi Mendoza Sep 03 '13 at 17:31
  • The problem seems to be the if statements. Either the param is being brought in wrong, type.equals doesnt work, or the string is wrong. – user2089255 Sep 03 '13 at 18:36
  • Move the logic to the controller, to many if-else replace with a map create the interface and call it for each instance of the map entry. Remember to always code to interfaces. – Roman C Sep 03 '13 at 19:10

3 Answers3

0

You are using type variable to get value from request parameter but in if condition you are using Page_type

INCLUDE JSP LIKE THIS

<div id="DYK_area">
   <jsp:include page="/imgs/random_dyk.jsp" flush="true">
     <jsp:param name="Page_type" value="prenatalOverviewArea" />
   </jsp:include>
</div> 

ON INCLUDED JSP

<%
    System.out.println("Page type is : "+request.getParameter("Page_type"));
    String Page_type = request.getParameter("Page_type");
    if(Page_type.equalsIgnoreCase("prenatalOverviewArea")){
        int random = (int )(Math.random() * 10 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }

    else if(Page_type.equalsIgnoreCase("prenatalNorm")){
        int random = (int )(Math.random() * 25 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
%><img id="dyk_random" src="<%=image_name%>"/>
Prateek
  • 12,014
  • 12
  • 60
  • 81
0

I'm not sure that the code is correct in /imgs/random_dyk.jsp. You use the variable Page_type although it doesn't exist.

You should initialize it before, at line request.getParameter("Page_type");like this:

<%! String xxx = request.getParameter("Page_type"); %>

then ,

<% if(xxx.equals("prenatalOverviewArea")){
...
Prateek
  • 12,014
  • 12
  • 60
  • 81
David Lakatos
  • 319
  • 3
  • 7
0

Here is what worked, thanks anyways guys, you all got me closer at the least!

<div id="DYK_area">
 <jsp:include page="/imgs/random_dyk.jsp" flush="true">
 <jsp:param name="page_type" value="prenatalOverviewArea"></jsp:param>
 </jsp:include>
</div>

<%@ page contentType="text/html; charset=UTF-8" language="java"  errorPage="" %>

<% String pageType = request.getParameter("page_type")==null?"": request.getParameter("page_type"); 
String image_name="";   
    if(pageType.equals("prenatalOverviewArea")){
        int random = (int )(Math.random() * 10 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("prenatalNorm")){
        int random = (int )(Math.random() * 25 + 1);
        image_name= "/images/did_you_know/dyk_p_" + random + ".png";
    }
    else if(pageType.equals("overviewArea")){
        int random = (int )(Math.random() * 20 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }
    else if(pageType.equals("overviewNorm")){
        int random = (int )(Math.random() * 45 + 1);
        image_name= "/images/did_you_know/dyk_" + random + ".png";
    }

if(!image_name.equals("")){
%>

<img id="dyk_random" src="<%=image_name%>"/>

<%}%>
user2089255
  • 87
  • 2
  • 12