Java File and JSP Page

I am to create a link verification program -- a simple java file with the database connection & some business logic and a JSP page that instantiates the my java file. It won't work. Everytime I run the jsp page, it gives me java.lang.NullPointerException. Can anyone give me some directions? The web server I am using is JRun.
Thank you
Below are my code:
JSP Page:
<%--      Verify Links
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="pro.*" %>
<html>
<head>
<title>Results</title>
</head>
<body>
<%
try{
LinkConnection newconn = new LinkConnection();
newconn.initialize();
newconn.query();
newconn.close();
}catch (ClassNotFoundException cnfe)
out.println(cnfe);
catch (SQLException sqle)
out.println(sqle);
catch (NullPointerException npe)
out.println(npe);
%>
</body>
</html>
Java Code:
package pro;
import java.io.*;
import java.sql.*;
import java.net.*;
public class LinkConnection {
Connection con;
ResultSet RS;
Statement Stmt;
public void initialize() throws SQLException, ClassNotFoundException
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("XXXX", "XXXX", "XXXX");
public void query() throws SQLException
Statement Stmt=con.createStatement();
String strSQL="Select guideline_name, guideline_url from pgt_guidelines";
ResultSet RS=Stmt.executeQuery(strSQL);
while (RS.next())
String guidelinename = RS.getString("guideline_name");
String guidelineurl = ("http://" + RS.getString("guideline_url"));
try {
URL url = new URL(guidelineurl);
HttpURLConnection link=(HttpURLConnection)url.openConnection();
System.out.println("<font color='blue'>");
System.out.println(guidelinename);
System.out.println("<br>");
System.out.println(guidelineurl);
System.out.println("</font>");
System.out.println("<font color='red'>");
System.out.println(link.getResponseCode());
System.out.println("</font>");
System.out.println("<br />");
          }catch (Exception e)
          {System.out.println("<font color='yellow'>Error Link </font>")
public void close() throws SQLException {     
RS.close();
Stmt.close();
con.close();

i'm having the same problem, what is the solution to this
A Servlet Exception Has Occurred
java.lang.NullPointerException
     at test.Lesson.runSql(Lesson.java:80)
===== Lesson.java file =====
package test;
import java.sql.* ;
import java.io.Serializable;
public class Lesson {
     public Lesson( ) { }
//Line 9
     public String runSql ( ) {
          String browserOutput = "";
          Connection sqlca = null;
          Statement sqlStatement = null;
          ResultSet myResultSet = null;
//Line 16
          /* Connect to database, run SQL and produce output */
          try { 
               /* Connection to the database */
               Class.forName("org.gjt.mm.mysql.Driver").newInstance();
               String theDatabase ="jdbc:mysql://localhost/employee";
               sqlca = DriverManager.getConnection(theDatabase,"usr","pwd");
//Line 24
               /* Construct and execute the sql, positioning before the
               first row in the result set */
               sqlStatement = sqlca.createStatement();
               myResultSet = sqlStatement.executeQuery
//Line 29
               ("select emp_id, " +
                    "emp_lname, " +
                    "city, " +
                    "state," +
                    "zip_code, " +
                    "emp_fname " +
                    "from employee " +
                    "where emp_id < 250 " +
                    "order by emp_lname, emp_fname");
//Line 39
               /* Construct the heading for the table */
               browserOutput =
                    "<div align=left>"+
                    "<table border=0 align=left width=520>" +
                    "<caption><i><b>" +
                    "Employee Listing</b></i></caption>" ;
//Line 46
               /* Construct the column headings for the table */
               browserOutput +=
                    "<th align=left>Emp_id</th>" +
                    "<th>First Name</th>" +
                    "<th>Last Name</th>" +
                    "<th>City</th>" +
                    "<th>State</th>" +
                    "<th>Zip</th>" ;
//Line 55
               /* Move to next row and & its contents to the html output */
               while(myResultSet.next()) {
                    browserOutput += "<TR><TD>" +
               myResultSet.getObject("emp_id").toString() + "</TD><TD>" +
               myResultSet.getObject("emp_fname").toString()+"</TD><TD>" +
               myResultSet.getObject("emp_lname").toString() + "</TD><TD>" +
               myResultSet.getObject("city").toString() + "</TD><TD>" +
               myResultSet.getObject("state").toString() + "</TD><TD>" +
               myResultSet.getObject("zip_code").toString() + "</TD></TR>" ;
//Line 66
               }//end of while
               sqlStatement.close();
               sqlca.close();
          }//end of try
          catch (SQLException e) {
//Line 72
               browserOutput = " Error: SQLException: " + e.getMessage();
               browserOutput= " Error: SQLState: " + e.getSQLState();
               browserOutput= " VendorError: " + e.getErrorCode();
          }//end of SQLException
          catch (Exception e) {
               browserOutput = " Error: JDBC Class creation: " + e.getMessage();
          }//end of Exception
/**Line 80**/     finally {
               try { sqlca.close(); }
               catch(SQLException e) {
               browserOutput = " Error: Closing connection: " + e.getMessage();
          }//end of finally
     /* Complete the html and return it to the Java Server Page */
     browserOutput += "</table>" + "</div>" ;
     return browserOutput;
     } //end of runSql
}//end of Lesson

Similar Messages

  • Import statement in .java files and .jsp files

    Guys I have few mysteries about this import statement.
    Please correct me if I am wrong.
    1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
    BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import the class I want to use.That is i have to say
    <%@page import="one"%>
    and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder.(Note that My.class is created in a folder different from c:\ClassFolder )
    But I am wondering why was there no need for me to import one.class in two.java.
    (Note I am using JDeveloper and Apache server)
    Please help.
    In this case the pSo I am wondering in JSP why it doesn't take the class I am looking for from the classpath .
    Help.

    You have to understand Java scoping rules. In the 1st scenario with the two Java files I am guessing neither one of them begins with a package statement. This means that they belong to the default package. When you compile the compiler uses the classpath to resolve method calls looking in the default package since there was no import statement specified in two.java. JSP is a little different. Now I just read this here in these forums a couple of days ago. Since the classfiles generated from the JSP files are not in the CLASSPATH they are loaded by a different classloader. This is why you have to import all references to all classes in the default package.

  • Import statement in .java files and .jsp files ..correction

    Guys I have few mysteries about this import statement.
    Please correct me if I am wrong.
    1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement in two.java I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
    BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import one.class in My.jsp .That is i have to say
    <%@page import="one"%>
    in My.jsp.
    and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder and MyJsp is compiled sucessfully.(Note that My.class is created in a folder different from c:\ClassFolder )
    But I am wondering why was there no need for me to import one.class in two.java.
    (Note I am using JDeveloper and Apache server)
    Please help.

    It has to do with packages. Most java classes are in a package, the name of which must conform to its place on the filesystem relative to the classpath. By that I mean that if you have com.mystuff.One.java, it must be in a folder com/mystuff where com is located somewhere in the classpath.
    What you've done is a little different. I'm assuming a couple of things:
    1. you have no package declaration at the top of one.java or two.java
    2. you have the current directory "." in your classpath.
    Java has the concept of the "default package", which covers classes without a declared package, and in your case is the current directory.
    So when you're in c:\sourcefolder and run the compiler, then "."="c:\sourcefolder", and that directory is part of the default package. No import statements are necessary for classes that are in the same package. This is why two.java can call methods in one.java without an import statement.
    When you run your jsp, the "current directory" part of your classpath is not c:\sourcefolder, but some other value (probably the directory you start your jsp engine from) You will have to import all non-java-library classes because the jsp itself becomes a java class, with a package that is determined by the jsp engine.

  • PAR files and jsp pages into NWDS

    Hi,
    I need to modify transactions from the MSS 50.x package, i.e. HR transactions. I need to delete some fields on the screens.
    The sysadmin was giving me a par file
    com.sap.pct.hcm.attendanceoverviewperiod.par.bak
    But when I import the par file into NWDS, the only thing I see is the portalapp.xml and a bunch of attribute files.
    No jsp page, no class files..
    When I browse into the directoria via sysadmin/support/portal runtime via WEB-INF, I do see all the necessary files. But how do I get the whole bunch of classes, jsp etc into NWDS? Download all single elements and put them manually into a NWDS project? There must be a better way
    Any help will be gracefully awarded

    I tried it, but when I follow your instruction, NWDS gives me an error in the import wizard "<dir> does not have a .project file".
    If I look in the .par file, there is no .project file at all.
    I looked in the download of the PCD, there is no .project file either, but at least the java classes and the jsp. 
    Is it removed intentionally? Probably because it is an SAP business package?

  • Problem accessing variables from java file to JSP page

    Hello Everyone,
    I have small problem accessing my method variables from my java bean file to a JSP page. can some please take a look and tell me why I can't populate my JSP page, I've been trying all day to get this done!!!
    This is my Java file
    package dev;
    import java.io.*;
    import java.util.*;
    public class RoundDetail2
    public String string_gameID;
    public int string_card1;
    public String readDetail_topLayer;
    public static final String SUITS[] = {" ", "Clubs", "Hearts", "Spades", "Diamonds", "Joker", "FaceDown"};
    public static final String VALUES[] = {"Joker ","Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    public static final String SuitVALUES[] = {" ","1","2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"};
    public RoundDetail2() throws FileNotFoundException
    DataInputStream inFile=
                 new DataInputStream(
                    new BufferedInputStream(
                   new FileInputStream("C:/apps/jakarta-tomcat-4.0.6/webapps/ROOT/WEB-INF/classes/dev/Data_452SY2.txt")));
    try { 
                 while((readDetail_topLayer = inFile.readLine()) != null)
              StringTokenizer tokenizer_topLayer = new StringTokenizer(readDetail_topLayer,"\t", true);
                   while  (tokenizer_topLayer.hasMoreTokens())
                        string_gameID = tokenizer_topLayer.nextToken();
         catch(IOException e)
                    System.out.println(e.getMessage());
         Thanks KT

    ......How are you declaring the class in your JSP? ... are you instantiating it as a bean using the useBean tag, or just instantiating it in your code like normal.
    Maybe you could post the relevant JSP code too?
    Hello again,
    Only the last string is populating after the file has be tokenized. What I'll like to accomplish is passing the very first string in the file. I did not get too far in the JSP file setup because the code is still in it's testing stage, but any help will be highly appreciated.
    Here is the JSP code
    <%@page import="dev.*" %>
    <%@page session="true" language="java" import="java.io.*" %>
    <jsp:useBean id="wagerhist" scope="request" class="dev.RoundDetail2" />
    <html>
    <head>
    <title>Round Detail</title>
    <body>
      <table width="530" bordercolor="#000000" valign="top">
        <tr>
              <td align="left"  width="19%">Game ID<%=wagerhist.string_gameID%></td>
              <td align="left"  width="30%">  </td>
              <td align="left"  width="20%">card1</td>
              <td align="left"  width="31%">  </td>
            </tr>
      </table>
    </body>
    </html>

  • How to run and displaying output of java file in jsp page ???

    Hai,
    I am going to develop a website which would have everything about java.
    Example code displayed for all methods.Next to every Example i shoud have *"Run"* button.
    Click at Run button should compile the example and show the output or even error.
    Ex:If the user clicks the vector link then all the examples will be shown..
    import java.util.*;
    etc...
    Run button-onclick it shoud compile the code above and shoud show the output....
    I don't know how to get the console error and output..
    kindly help me out....Thanks.

    Check java.lang.Runtime API.

  • Ordinary java class and jsp page

    hi i want to use some java class(not javabean ) in my jsp . how do i import them and use them?

    You can just import the package into your JSP, and access it like any other class of the Standard Java API that you access.
    fun_one

  • Passing vector between java class and jsp page

    Hi,
    I think this is similar to the last post but anyways maybe somebody can help us both out bontyh.....I am getting an error saying undefined variable or class when i try to access a Vector which was returned in a class.
    Anyways anyone got any ideas?

    Maybe if you give some more information...

  • Java code and jsp interaction

    Hi,
    How does one interact between java code and jsp page.That is, if the java code does say database connection, the web page should tell that the connection has been made,just like an application but this will work on the web server not in the console.Kindly let me know.
    As

    Just curious but where do these questions come from? A course you are taking or you are building something for a hobby?
    If you are building some small simple web-app it's probably quicker to embed code right onto your JSP page.
    There are various levels of abstracting your web-app separating display from logic from the model.
    The following are techniques from simple web-apps to more complicated ones.
    -Embedding script right into your JSP
    -Using beans
    -Using custom tags ( I think there are some standard ones now, struts seems to be a popular buzzword)
    -Including a servlet to handle your requests redirect, update bean content grab info form your database and forward to a JSP. (Read up on model 2 architecture.)
    To include as much detail as possible would be to write a book. Fortunately many have so I don't have to fit one in this tiny reply box.
    Hope that was somewhat useful.

  • Serving Java Servlets and JSP

    I have a small hosting company and was wondering what is required to be installed on a Win2k Server to host Java Servlets and JSP pages for a client of mine?

    Ah, so you just want to add a servlet engine to IIS5?
    Tomcat can be used as a plugin for IIS. Check out the Tomcat FAQs - somewhere in there you should find one relating to using Tomcat as an IIS plugin. They're far more comprehensive than I could ever hope to be on the matter!

  • How can i call java class file in jsp page

    Hai,
    i wants to call .class file in jsp page.
    my class file is in C:\jsdk\bin.
    Thanks

    I'm not entirely sure what you mean by "calling a class file", but I'm going to assume that you want to do something like the following in your page:
    <%
    MyClass myClass = new MyClass();
    myClass.someMethod();
    %>etc
    If that's the case, then all you have to do is make sure that the class is in the servlet engine's classpath. You'll probably also need to include an appropriate import statement at the top of the page.
    Hope that helps.

  • Java Classes and JSP`s (urgent)

    hi, my question is:
    How can i invoke normal java classes` methods using Jsp`s for this??, is it possible??, if it is, then how do you do this, and where do you have to put the java classes in order to find them from a jsp.
    thanks

    Hi,
    You can access normal java classes in JSP pages. You have to import them in the header like this:
    <%@ page language="java" import="//your java class file with package"%>
    or if u r using java beans, then use the <usebean> tag. Put the java classes in the classes directory. Which server u r using? If it is JWS, then u have a specified classes directory, where u have to put all the class files along with their packages. Hope this will help.
    atanu.

  • How to load java class from jsp page?

    hi all!
    Does anyone know how to load java class from jsp page?
    I try to load java class from jsp page.
    Is it possible to load java class fom jsp page?
    thanks and have a good day!

    What I mean is How to load/open java class file from jsp page?
    I think we can open Applet from jsp page by using
    <applet code=helloApplet.class width=100 height=100>
    </applet>
    but, how to open java class which is an application made by Frame?
    thanks and have a good day

  • Calling java class from jsp page

    Dear Friends.
    I wrote jsp page and java class.
    Am calling java class from jsp page. after processing result,
    I have to refresh jsp page from java class.
    processing time may take 5 minutes or 1 minute etc. that depends on user.
    Can It be possible ? if possible , How ?

    Ok, I get a very strange error now:
    org.apache.jasper.JasperException: Unable to compile class for JSPerror: An error has occurred in the compiler; please file a bug report (http://java.sun.com/cgi-bin/bugreport.cgi).
    What is this??? Anyone?

  • Forwarding to an xml file from jsp page

    hi
    i am subhash. i have problem in forwarding an xml file from jsp page.
    i have a jsp page in which i have a button. upon click, its form method is triggered and the action is set to "serverpage.jsp". In that jsp page i have to move to an xml file. I would like u to send a sample code also for the same.
    Thank You,
    Subhash..

    Refer to the Generating XML from JSP section.
    http://java.sun.com/developer/technicalArticles/xml/WebAppDev2/

Maybe you are looking for

  • Report for open purchase order quantity

    Dear experts, Is there any report for showing open purchase order quantity for which goods are not yet received? Regards babu

  • How can I get the love back?

    Remember the anthem in 1984? The call for independence? The hammer that shattered the drone which kept us all in line - servants to the man? I do. I begged the hammer to be thrown and cheered when the glass shattered. I evangelized for the awakening

  • How do you get rid of unwanted alarm sound songs?

    Hey guys.  I'm stumped.  I have an iphone 5 that I recently set back to factory settings so it's cleared out of music.  And I don't sync my phone with my itunes library on my macbook as I don't want music on my phone.  BUT I noticed there's 246 songs

  • Image larger than specified

    Hey guys, I started a new project. I set the file criteria for 4" wide 5" high, 300 resolution, CMYK. When I save the file as a JPEG and export it for print the image is huge. I don't know what I'm doing wrong. I check the settings again, after notic

  • How to understand func spec

    Can u tell me the better way of understanding and analyzing the functional specification