jsp请求.获取参数

vngu2lb8  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(157)

编辑,这仍然不是完全工作,但我需要在网站的每个页面上设置一个page_type。该page_type需要由一些java检查并基于page_type进行编码。问题是,if else语句没有正确完成。什么也没有发生,因为参数似乎没有被放置。

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

<%}%>
mkshixfv

mkshixfv1#

您正在使用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>

在包含的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%>"/>
nhjlsmyf

nhjlsmyf2#

我不确定/imgs/random_dyk. jsp中的代码是否正确。您使用了变量Page_type,尽管它并不存在。
您应该在request.getParameter("Page_type");行之前对其进行初始化,如下所示:

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

然后,

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

wztqucjr3#

这是什么工作,无论如何谢谢伙计们,你们都让我更接近至少!

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

<%}%>

相关问题