我有两个文件abc.jsp和def.jsp。在abc.jsp中,它包含以下代码:
<%
String selectedCourse = request.getParameter("course");
int count = 0;
if(selectedCourse.equals("PHP")){
count = count + 1;
}
%>
我想要def.jsp中的此计数值,那么如何传递此值?
答案 0 :(得分:1)
abc.jsp
<%!
private synchronized void incrementCounter() {
Integer count = session.getAttribute("count");
if (count == null) {
count = new Integer(0);
}
count++;
session.setAttribute("count", new Integer(count));
}
%>
<%
String selectedCourse = request.getParameter("course");
if(selectedCourse.equals("PHP")){
incrementCounter();
}
%>
def.jsp
<%
int count = session.getAttribute("count");
%>
无论如何,这种逻辑不应在JSP中,而应属于控制器(servlet)。
答案 1 :(得分:0)
使用会话属性
abc.jsp
<%
String selectedCourse = request.getParameter("course");
int count = 0;
if(selectedCourse.equals("PHP")){
count = count + 1;
}
session.setAttribute("count", count);
%>
def.jsp
int count1 = Integer.parseInt(session.getAttribute("count").toString());
out.write("count:"+count1);