Solved

Query from url string?

Posted on 2001-07-05
7
217 Views
Last Modified: 2010-04-16
Hello,

  I have a problem, probably is pretty simple. I generate page using the code below. It makes links to ProbId's. These ProbId's are in my DB and when a user clicks on the link it should then generate another page using the ID number I embedded into the link. How do I make that part work?  Would I make a second java class to handle this or within this same class? Please give an example based on this example.

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowLinks extends HttpServlet {
     public void doGet(HttpServletRequest request, HttpServletResponse response)
     throws IOException, ServletException {
          try {

               Class.forName("oracle.jdbc.driver.OracleDriver");
               String url = "jdbc:oracle:thin:@site:1521:webSite";
               Connection con = DriverManager.getConnection(url, "SYS", "CHANGEME");
               Statement stmt1 = con.createStatement();
               ResultSet rs1;    

               response.setContentType("text/html");
               PrintWriter out = response.getWriter();
               out.println("<html>");
               out.println("<body>");
               out.println("<head>");
               out.println("<title>Servlet demo</title>");
               out.println("</head>");
               out.println("<body>");
               out.println("<h3><Center>Show data.</Center></h3>");
               //Show data
               out.println("<table border=1>");
               // Create Header Row
               out.println("<TR>");
               out.println("<TD><B>No</B></TD>");
               out.println("<TD><B>ProbID</B></TD>");
               out.println("<TD><B>Name</B></TD>");
               out.println("<TD><B>Entry Date</B></TD>");
               out.println("<TD><B>Modify Date</B></TD>");
               out.println("</TR>");
               

               String Val = new String();
               rs1 = stmt1.executeQuery("Select NO, Probid, name, ENTRYDATE, TO_CHAR(MODIFYDATE,'MM/DD/YYYY')From tbl_1 ");

               while(rs1.next())
               {
                    out.println("<TR>");
                    Val = rs1.getString(1);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(2);
                    out.println("     <TD><a href='www.site.com/?ID='" + Val + ">" + Val + "</a></TD>");
                    Val = rs1.getString(3);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(4);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(5);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(6);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(7);
                    out.println("     <TD>" + Val + "</TD>");
                    Val = rs1.getString(8);
                    out.println("     <TD>" + Val + "</TD>");
                    out.println("</TR>");
                  }
               out.println("</table>");
               out.println("</body>");
               out.println("</html>");
          }
          catch (IOException e)
          {
               System.out.println(e);
          }
          catch (SQLException e)
          {
                       e.printStackTrace();
          }
          catch (Exception e)
          {
                       e.printStackTrace();
          }    
     }
}
0
Comment
Question by:MrError
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6258076
> Would I make a second java class to handle this or
> within this same class?

It depends on what the second page is doing/displaying.
It can be implemented as either depending on your needs, but from the sounds of it a second servlet would be best.
This servlet would get the ID value from the servlet context, make the required database query and output the results.
If you were doing it in the same servlet then you would check for the ID value in the context, and if it is present then handle as I described above, and if it was not prenet then generate the list as you have already done.
0
 

Author Comment

by:MrError
ID: 6258095
Objects,

Yes I think a second class would be excellent. The info would be more details about the problem that occured. How it was resolved who handled it etc... Could you should how this second class would look like? It would have a very similiar table appearance like the first page. The part I am most confused by is how this second class will use the url which has the problem Id contained within it for the Sql.

-MrError
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6258109
You can just use request.getParameter("ID") to retrieve the ID value, and the rest is along the lines of what you've already done in your existing servlet.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:MrError
ID: 6258183
So would I put inside the links a call to the second servlet say it is called Getlink... then the first class would make the links like this:

out.println("<TD><a href='www.site.com/GetLink?ID='" + Val + ">" + Val + "</a></TD>");

This then calls GetLink servlet? Does this look right?

GetServlet uses the ID like this:

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GetLink extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
         try {

           Class.forName("oracle.jdbc.driver.OracleDriver");
              String url = "jdbc:oracle:thin:@site:1521:webSite";
              Connection con = DriverManager.getConnection(url, "SYS", "CHANGEME");
              Statement stmt1 = con.createStatement();
              ResultSet rs1;    

              response.setContentType("text/html");
              PrintWriter out = response.getWriter();
              out.println("<html>");
              out.println("<body>");
              out.println("<head>");
              out.println("<title>Servlet demo page2</title>");
              out.println("</head>");
              out.println("<body>");
              out.println("<h3><Center>Show detail</Center></h3>");
              //Show data
              out.println("<table border=1>");
              // Create Header Row
              out.println("<TR>");
              out.println("<TD><B>ProbID</B></TD>");
              out.println("<TD><B>Name</B></TD>");
              out.println("<TD><B>Desc</B></TD>");
              out.println("</TR>");
             
          PId = request.getParameter("ID");

              String Val = new String();
              rs1 = stmt1.executeQuery("Select Probid, fixedby, desc From tbl_2 where ProbId ="+ PId);

              while(rs1.next())
              {
                   out.println("<TR>");
                   Val = rs1.getString(1);
                   out.println("     <TD>" + Val + "</TD>");
                   Val = rs1.getString(2);
             out.println("     <TD>" + Val + "</TD>");
                   Val = rs1.getString(3);
                   out.println("     <TD>" + Val + "</TD>");
                   out.println("</TR>");
                 }
              out.println("</table>");
              out.println("</body>");
              out.println("</html>");
         }
         catch (IOException e)
         {
              System.out.println(e);
         }
         catch (SQLException e)
         {
                      e.printStackTrace();
         }
         catch (Exception e)
         {
                      e.printStackTrace();
         }    
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 6258193
Looks about right to me :)
0
 

Author Comment

by:MrError
ID: 6258238
ok.. I have my second page being called now. Getlink shows up, the string was slightly wrong but this is correct:

out.println("     <TD><a href='http://www.site.com:8080/GetLink?ID=" +Val+ "'>" +Val+ "</a></TD>");

thank you for the help.

0
 
LVL 92

Expert Comment

by:objects
ID: 6258254
No worries.
Thanks for the points :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now