Project

General

Profile

« Previous | Next » 

Revision 46

Added by Matt Jones over 24 years ago

started servlet to do XML query and return result set

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1
import java.io.PrintWriter;
2
import java.io.IOException;
3
import java.util.Enumeration;
4
import java.util.Hashtable;
5

  
6
import javax.servlet.ServletConfig;
7
import javax.servlet.ServletContext;
8
import javax.servlet.ServletException;
9
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12

  
13
/**
14
 * A metadata catalog server implemented as a Java Servlet
15
 */
16
public class MetaCatServlet extends HttpServlet {
17

  
18
  private ServletConfig		config = null;
19
  private ServletContext	context = null;
20
  DBSimpleQuery		queryobj = null;
21
  static  String 	user = "jones";
22
  static  String 	password = "your-pw-goes-here";
23
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
24

  
25
  public void init( ServletConfig config ) throws ServletException {
26
    try {
27
      super.init( config );
28
      this.config = config;
29
      this.context = config.getServletContext();
30
    
31
      try {
32
        queryobj = new DBSimpleQuery(user, password, defaultDB);
33
      } catch (Exception e) {
34
      }
35
    } catch ( ServletException ex ) {
36
      throw ex;
37
    }
38
  }
39

  
40
  public void doGet (HttpServletRequest request, HttpServletResponse response)
41
    throws ServletException, IOException {
42

  
43
    PrintWriter out;
44
    String query = "Value1";
45

  
46
    // Run the query
47
    Hashtable nodelist = queryobj.findRootNodes(query);
48

  
49
    // set content type and other response header fields first
50
    response.setContentType("text/xml");
51

  
52
    // then write the data of the response
53
    out = response.getWriter();
54

  
55
    // Print the reulting root nodes
56
    long nodeid;
57
    out.println("<?xml version=\"1.0\"?>\n");
58
    out.println("<resultset>\n");
59
    Enumeration rootlist = nodelist.keys(); 
60
    while (rootlist.hasMoreElements()) {
61
      nodeid = ((Long)rootlist.nextElement()).longValue();
62
      out.println("  <nodeid>" + nodeid + "</nodeid>");
63
    }
64
    out.println("</resultset>");
65

  
66
    out.close();
67
  }
68

  
69
  public void doPost( HttpServletRequest req, HttpServletResponse res )
70
    throws ServletException, IOException {
71
  }
72

  
73
}
0 74

  
src/edu/ucsb/nceas/metacat/DBSimpleQuery.java
93 93
   * Generally, one would call the findRootNodes() routine after creating 
94 94
   * an instance to specify the query to search for
95 95
   *
96
   * @param query the text to search for in the element and attribute content
97 96
   * @param user the username to use for the database connection
98 97
   * @param password the password to use for the database connection
99 98
   * @param dbstring the connection info to use for the database connection
100 99
   */
101
  private DBSimpleQuery( String user, String password, String dbstring) 
100
  public DBSimpleQuery( String user, String password, String dbstring) 
102 101
                  throws IOException, 
103 102
                         SQLException, 
104 103
                         ClassNotFoundException
MetaCatServlet.java
1
import java.io.PrintWriter;
2
import java.io.IOException;
3
import java.util.Enumeration;
4
import java.util.Hashtable;
5

  
6
import javax.servlet.ServletConfig;
7
import javax.servlet.ServletContext;
8
import javax.servlet.ServletException;
9
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12

  
13
/**
14
 * A metadata catalog server implemented as a Java Servlet
15
 */
16
public class MetaCatServlet extends HttpServlet {
17

  
18
  private ServletConfig		config = null;
19
  private ServletContext	context = null;
20
  DBSimpleQuery		queryobj = null;
21
  static  String 	user = "jones";
22
  static  String 	password = "your-pw-goes-here";
23
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
24

  
25
  public void init( ServletConfig config ) throws ServletException {
26
    try {
27
      super.init( config );
28
      this.config = config;
29
      this.context = config.getServletContext();
30
    
31
      try {
32
        queryobj = new DBSimpleQuery(user, password, defaultDB);
33
      } catch (Exception e) {
34
      }
35
    } catch ( ServletException ex ) {
36
      throw ex;
37
    }
38
  }
39

  
40
  public void doGet (HttpServletRequest request, HttpServletResponse response)
41
    throws ServletException, IOException {
42

  
43
    PrintWriter out;
44
    String query = "Value1";
45

  
46
    // Run the query
47
    Hashtable nodelist = queryobj.findRootNodes(query);
48

  
49
    // set content type and other response header fields first
50
    response.setContentType("text/xml");
51

  
52
    // then write the data of the response
53
    out = response.getWriter();
54

  
55
    // Print the reulting root nodes
56
    long nodeid;
57
    out.println("<?xml version=\"1.0\"?>\n");
58
    out.println("<resultset>\n");
59
    Enumeration rootlist = nodelist.keys(); 
60
    while (rootlist.hasMoreElements()) {
61
      nodeid = ((Long)rootlist.nextElement()).longValue();
62
      out.println("  <nodeid>" + nodeid + "</nodeid>");
63
    }
64
    out.println("</resultset>");
65

  
66
    out.close();
67
  }
68

  
69
  public void doPost( HttpServletRequest req, HttpServletResponse res )
70
    throws ServletException, IOException {
71
  }
72

  
73
}
0 74

  
Makefile
66 66
		DBSAXHandler.java \
67 67
		DBReader.java \
68 68
		ReaderElement.java \
69
		DBSimpleQuery.java
69
		DBSimpleQuery.java \
70
		MetaCatServlet.java
70 71

  
71 72
clean:
72 73
	-rm -f *.class Log

Also available in: Unified diff