?
Solved

Query from url string?

Posted on 2001-07-05
7
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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