问一个在jsp页面里引入java文件的路径问题
在src目录的board.wl.com包里面有个boardinfo.java文件,如下package board.wl.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Boardinfo {
private Connection ct=null;
private Statement st=null;
private ResultSet rs=null;
public ResultSet getBoard(){
try{
Class.forName("com.mysql.jdbc.Driver");
ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user","root","523325");
st=ct.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery("select BoardName from board");
}catch (Exception e){
e.printStackTrace();
}
return rs;
}
}
在WebRoot目录下有loginsuccess.jsp文件,想在此jsp文件中引入上面的java代码,用<%@ include file=""%> 指令,可是我不知道路径该怎么写?
请高手指教
------解决方案--------------------这样写
<%
private Connection ct=null;
private Statement st=null;
private ResultSet rs=null;
public ResultSet getBoard(){
try{
Class.forName("com.mysql.jdbc.Driver");
ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user","root","523325");
st=ct.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery("select BoardName from board");
}catch (Exception e){
e.printStackTrace();
}
return rs;
%>
------解决方案--------------------LZ 可以先建一个页面 a.jsp,把那个类的东西放到里面 如
<%
private Connection ct=null;
private Statement st=null;
private ResultSet rs=null;
public ResultSet getBoard(){
try{
Class.forName("com.mysql.jdbc.Driver");
ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user","root","523325");
st=ct.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery("select BoardName from board");
}catch (Exception e){
e.printStackTrace();
}
return rs;
}
%>
再建一个页面 b.jsp ,现在可以用 <%@ include file="a.jsp" %>
这个按LZ的想法可以实现。但建议直接写到一个页面中,把类代码写到<% %>中,再下面的页面中调用它的方法即可,可以不用<%@ %> 直接写
试试吧