Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Turning a JSP into a JAVA file

  1. #1
    PyroPlasm is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Turning a JSP into a JAVA file

    Hello, I have a class the purpose of which is to grab the HTML from a given URL or set of URLs that I hardcode in it. I have a JSP that I was given that already does this. I was told that I need to put it in a normal Java file so I can run it and test it and my code can be put where it belongs by another developer. So far I've worked a lot of the kinks out but I'm a little confused by what I got on another forum. Below is my code:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Locale;
    
    
    public class htmlOutput {
    
    	/**
    	 * @param args
    	 */
    
    
    	public static void main(String[] args) {
    		pageContext.setAttribute("enBean","en");
    		
    		Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
    		if (locale == null) {
    			locale = new Locale(Constants.LOCALE_EN);
    		}
    		
    		// Setting the session attribute indicating entry from index page
    		session.setAttribute(Constants.ENTER_FROM_INDEX, new Boolean(true));
    		
    		URL headerURL;
    		URLConnection headerURLConnection;
    		BufferedReader br;
    		
    		String inputLine;
    		
            try {
                headerURL = new URL("*");
                headerURL = new URL("*");
                headerURL = new URL("*");
                headerURLConnection = headerURL.openConnection();
                br = new BufferedReader(new InputStreamReader(headerURLConnection.getInputStream()));
    
                while ((inputLine = br.readLine()) != null) {
                    System.out.println(inputLine);
                }
                
            } catch (MalformedURLException me) {
                System.out.println("MalformedURLException: " + me);
            } catch (IOException ioe) {
                System.out.println("IOException: " + ioe);
            }finally{
            	br.close();
            }
            
            //Process proc = Runtime.getRuntime().exec("./html_compare.ksh");
    
    	}
    
    }
    It is giving me some errors on pageContext, session, Globals, Constants, and out saying they cannot be resolved or cannot be resolved to a variable and I was told by extracting a delegate class to do the work you avoid pageContext and session and for the most part you avoid Globals and Constants. They said I would need to create some new methods for this but didn't give much more information to which I asked:

    I know I'll need multiple methods. I guess the thing that confuses me are things like:

    pageContext.setAttribute("enBean","en");

    Should pageContext be a method and therefore this would be an object of that method?

    Also:
    Java Code:
    Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
    if (locale == null) {
    locale = new Locale(Constants.LOCALE_EN);
    For this what should session, Globals and Constants be? Should they also be separate methods and therefore I'm calling objects?

    Any help here would be greatly appreciated.
    Last edited by PyroPlasm; 01-24-2012 at 07:41 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Turning a JSP into a JAVA file

    DarrylBurke likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Turning a JSP into a JAVA file

    Thanks for the heads-up, Kevin.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Turning a JSP into a JAVA file

    Strip out all the server and JSP specific code like pageContext etc to get some code that will compile and execute.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Turning a JSP into a JAVA file

    Do you know anything about JSPs, servlets and how they all work inside a container (eg Tomcat)?
    Because you should realise (as Norm suggests) that most of that initial stuff is not necessary for the task at hand.

    Also, from your description, is this supposed to be a standalone app, or a utility class that can be used from elsewhere? If the latter than sticking this in a main() method strikes me as the wrong way to be going about this...indeed, either way it is probably the wrong way to tbe going about this.

Similar Threads

  1. Combo box trouble turning into a boxing match with java
    By Embercloud in forum New To Java
    Replies: 1
    Last Post: 01-05-2011, 03:46 PM
  2. turning an ArrayList into an Array
    By imorio in forum New To Java
    Replies: 3
    Last Post: 01-01-2011, 04:58 PM
  3. turning off key-repeat
    By gib65 in forum Advanced Java
    Replies: 4
    Last Post: 08-25-2010, 02:58 PM
  4. Turning a Bug around.
    By Nightshadow_t2 in forum New To Java
    Replies: 3
    Last Post: 02-17-2010, 08:03 AM
  5. Replies: 3
    Last Post: 08-11-2009, 12:18 AM

Posting Permissions

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