Results 1 to 3 of 3
  1. #1
    dsagrera is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Thumbs down Initializing a class from a JSP page

    Hi guys, I'm new to Java, and I really mean NEW. I'm about to start a Java course in a few days, but I can't wait to get started, so I did start. Here's my issue.

    I've set up Tomcat and installed Eclipse. I've also created a class to download the source code of a page:

    Java Code:
    package myPackage;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    @WebServlet("/GetPageSource")
    public class GetPageSource extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        public GetPageSource() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response, String url) throws ServletException, IOException {
    	  URL SourceCode = new URL(url);
        URLConnection yc = SourceCode.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        StringBuffer results = new StringBuffer();
        String inputLine;
        while ((inputLine = in.readLine()) != null) { 
          results.append(inputLine);
        }
        in.close();
        PrintWriter out = response.getWriter();
        out.print(results.toString());
        //return results;
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    	}
    }
    I've also created a JSP page and deployed the project as a WAR file and set it correctly on Tomcat. The page is showing up just fine in my local server. Now I want to get the source code of a page and show it on my JSP page, but I try and try and nothing seems to work. I swear I've tried *almost* everything (the olny option missing seems to be the working one)

    Any help? Please? Regards.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default Re: Initializing a class from a JSP page

    Hi. Can you do without jee technologies?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,447
    Rep Power
    18

    Default Re: Initializing a class from a JSP page

    Java Code:
    protected void doGet(HttpServletRequest request, HttpServletResponse response, [B]String url[/B]) throws ServletException, IOException {
        URL SourceCode = new URL(url);
        URLConnection yc = SourceCode.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        StringBuffer results = new StringBuffer();
        String inputLine;
        while ((inputLine = in.readLine()) != null) { 
            results.append(inputLine);
        }
        in.close();
        PrintWriter out = response.getWriter();
        out.print(results.toString());
        //return results;
    }
    You've changed the signature of the doGet method.
    That "String url" part isn't there in the real doGet().
    So I would guess that code is never being called.

Similar Threads

  1. Error initializing class instances
    By fr0s1yjack in forum New To Java
    Replies: 1
    Last Post: 06-22-2011, 03:58 PM
  2. Initializing FileWriter
    By loopsnhoops in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2011, 02:28 AM
  3. how to Import my Java Class in Jsp Page
    By pos in forum JavaServer Pages (JSP) and JSTL
    Replies: 20
    Last Post: 04-06-2010, 02:51 PM
  4. How do I call a class in jsp page
    By Greg in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-20-2009, 10:27 PM
  5. Replies: 0
    Last Post: 08-10-2009, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •