JSP Page Import problem. Class file placed in a package inside WEB-INF/classes

梦想的初衷 提交于 2019-12-23 03:19:30

问题


I have a Web application crawler_GUI running which has another java project jspider in its buildpath. (I use eclipse galileo)

The GUI uses the jspider project as its backend.

Visit http://i45.tinypic.com/avmszn.jpg for the structure

The JSP creates an instance of the jspider object. First of all I didn't have the classes in the WEB-INF/classes folder and I rectified that error. Now it seems to work, and no errors are shown but none of the tasks are carried out.

Here's the code :

The JSP

<%@ page import = "net.javacoding.jspider.ESpider, source.Crawler"%>
<%@ page import = "java.net.URL" %>
<%//URL baseURL = new URL(Crawler.SelectedSites.get(0));
URL baseURL = new URL("http://www.buy.com");
System.out.println("******");
ESpider espider = new ESpider(baseURL);

The *s get printed.

ESpider.java

public ESpider(URL baseURL) throws Exception {
    super(baseURL);
    System.out.println("test");


}

It doesn't print "test". In fact the parent's constructor isn't even being called. At the same time no errors are displayed either.

How can I fix this?


回答1:


In Eclipse, you need to add the jspider project to the crawler_GUI project as follows:

  1. crawler_GUI properties > Java Build Path > Projects > Add jspider.
  2. crawler_GUI properties > Java EE Module Dependencies > Tick jspider.

Don't forget to cleanup any loose files in /WEB-INF/classes you've added manually. This is unnecessary. Eclipse will automagically take care about this if you referenced the projects the right way. Also, any loose JAR files are supposed to be just dropped in /WEB-INF/lib.

Now the JSP part of the story. It's hard to pinpoint the root cause since you wrote raw Java code in a JSP file instead of a real Java class. First step would be checking the server logs for any inconsistencies. It may also have happened that the wrong version of the ESpider class has been loaded (which lacks the sysout).

As already hinted, this isn't really the way you're supposed to use JSP. It is to be used to as a template to write HTML/CSS/JS in wherein you can dynamically control the flow with help of taglibs like JSTL and access backend data using EL. Raw Java code belongs in Java classes, not JSP files. In this case you should have used a Servlet class. Just create a class which extends HttpServlet, implement the doGet() method accordingly with the ESpider stuff and finally forward to a JSP page to display the result, register the servlet in web.xml and call it by URL which covers its url-pattern in web.xml. You can find here a lot of good JSP/Servlet tutorials.

P.S: ensure that you understand the robots.txt policy...



来源:https://stackoverflow.com/questions/2303412/jsp-page-import-problem-class-file-placed-in-a-package-inside-web-inf-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!