开发者

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

开发者 https://www.devze.com 2022-12-21 05:15 出处:网络
I have a Web application crawler_GUI running which has another java project jspider in its buildpath. (I use eclipse galileo)

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 J开发者_JAVA百科SP 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?


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号