Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query from url string?

Posted on 2001-07-05
7
Medium Priority
?
226 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

636 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