jsp request.getParameter

时间:2013-09-03 16:40:33

标签: java jsp

已编辑,但这不完全正常,但我需要在网站的每个页面上设置page_type。该page_type需要由某些java检查并根据page_type执行代码。 问题是,if else语句没有正确完成。没有任何事情发生,因为似乎没有放置参数。 正在设置page_type的主页面:

<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>

正在检查代码

<% 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%>"/>

<%}%>

3 个答案:

答案 0 :(得分:0)

您正在使用type变量从请求参数获取值,但在if条件下您正在使用Page_type

包含这样的JSP

<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%>"/>

答案 1 :(得分:0)

我不确定/imgs/random_dyk.jsp中的代码是否正确。您使用变量Page_type虽然它不存在。

您应该在request.getParameter("Page_type");行之前对其进行初始化:

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

然后,

<% if(xxx.equals("prenatalOverviewArea")){
...

答案 2 :(得分:0)

这是有效的,谢谢你们,你们至少让我更近了!

<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%>"/>

<%}%>