I have created a dynamic web project in eclipse.
它包含一个Servlet ResidentApi.java
和两个java类:GeoLocationDemo.java
和Geolocation.java
。我从我的servlet调用GeoLocationDemo.java
并在ResultSet中得到结果。但是我没有在ResultSet中获得任何值。
当我分别运行GeoLocationDemo.java
时,我得到了正确的结果。我不知道servlet是否可以调用我的java类,但如果是,那么为什么我没有得到结果。
我很难调试它。我正在做的是每次在tomcat服务器上运行这个项目的.war文件并检查结果。请建议一个好方法在eclipse上测试它。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ResultSet rs = null;
try{
GeoLocationDemo geo = new GeoLocationDemo(); //Here i created a new object
rs = geo.getResults(); //here i called a method of GeoLocation
}catch(Exception e){
// System.out.println(e);
out.write("<head><b You suck</b></head>");
}
out.write("<head><b>Congratulation! connected</b></head>"); //i am getting this output
try{
while(rs.next()){
String s = rs.getString("Details");
out.write("<head><b> "+s+ " </b></head>"); //not able to get this output
}
}catch(Exception e){
// System.out.println(e);
out.write("<head><b>You Built </b></head>");
}
out.close();
}
答案 0 :(得分:0)
不要将所有内容都放在<head>
标记中!在某个地方打开body
。不要无声地吞下你可能会得到的任何Exception
。请记得close()
ResultSet
。此外,您可能应该使用POJO在List
中返回所有数据。
out.write("<body>");
out.write("<b>Congratulation! connected</b><br/>");
try {
while (rs.next()) {
String s = rs.getString("Details");
out.write("<b> "+s+ "</b><br/>");
}
} catch (Exception e){
// System.out.println(e);
out.write("<b>" + e.getMessage() + "</b>");
e.printStackTrace(out);
} finally {
try {
out.close();
} catch (Exception ignored) {
}
try {
rs.close();
} catch (Exception ignored) {
}
}
答案 1 :(得分:0)
P.S。不要关闭你从未打开的资源 - out.close();
- 它由servlet容器管理