Importing 2 proxies on JSP
I'm having problems with my code. I need to import two proxies on my JSP. I don't know why but my code gives an error whenever import my other proxy package. I thought of putting the import codes in an if else statement but it doesn't work. Is there something wrong with my code?
<%@ page language="java" %>
<%@ page import="java.lang.String"%>
<%
int i = 1;
if (i == 0){
%>
<%@ page import ="com.x.x.function" %>
<%@ page import ="com.x.x.functionViDocument" %>
<%@ page import ="com.x.x.types.*" %>
<%
}else {
%>
<%@ page import ="com.x.x2.function" %>
<%@ page impor开发者_开发百科t ="com.x.x2.functionViDocument" %>
<%@ page import ="com.x.x2.types.*" %>
<%
}
%>
You can't do conditional imports in Java. The JSP is converted to a java class (that extends HttpServlet), and your code is not a valid java code.
You can use fully-qualified class names in your code instead. If you need a Function
, then:
com.x.x.Function fn1 = ...
and com.x.x2.Function fn2 = ..
.
However, this is not a good practice. Preferably you should have an interface that both Function
classes implement, so that you don't have so much conditional copy-pasted code.
Two other things: use capitalized class names (Function
instead of function
). And don't write java code in JSPs. Use a servlet instead.
精彩评论