0

So I'm trying to do some basic stuff with databases and graphs. I've gotten my JSP code working, but I want to move it over to a servlet/other resources because I've heard that doing everything in a jsp is a very bad idea. I've done some research on servlets but I'm very confused by how they work/link up with jsps and other files.

I know that I'm going to want to keep the d3.js code in the .jsp (probably), but I'm mainly concerned about the database connectivity code.

Here is my JSP:

    <%@ page language="java" import="java.sql.*, java.util.*"%>

<html>
    <head>
        <title>D3 Test</title>
        <script type="text/javascript" src="d3/d3.v2.js"></script>
        <style type="text/css">
        </style>
    </head>
    <body>

    <%
    Class.forName("com.mysql.jdbc.Driver");



Connection con=null;
ResultSet rst=null;
Statement stmt=null;
try{
String url="jdbc:mysql://localhost:3306/testdb?user=root&password=password";

int i=1;
con=DriverManager.getConnection(url);
stmt=con.createStatement();
rst=stmt.executeQuery("select * from test ");

%>
        <script type="text/javascript">

            var dataset = [ 
    <% while (rst !=null && rst.next()) { %>
       <%=rst.getInt("ID")%>, 
    <% }%>
    ]; 

            var w = 500;
            var h = 340;
            var barPadding = 1;
            /*var xScale = d3.scale.linear()
                                 .domain([0, d3.max(dataset, function(d) { return d[0]; })])
                                 .range([padding, w - padding * 2]);

            var yScale = d3.scale.linear()
                                 .domain([0, d3.max(dataset, function(d) { return d[1]; })])
                                 .range([h - padding, padding]);*/
            var svg = d3.select("body")
                        .append("svg")
                        .attr("width", w)
                        .attr("height", h);
            svg.selectAll("rect")
                .data(dataset)
                .enter()
                .append("rect")
                .attr("id", "rect1")
                .attr("x", function(d, i) {
                        return i * (w / dataset.length);
                    })
                .attr("y", function(d) {
                    return h - d*4;
                })
                .attr("width", w/ dataset.length - barPadding)
                .attr("height", function(d) {
                    return d * 4;
                })
                .attr("fill", function(d) {
                    return "rgb(0, 0, " + (d * 10) + ")";
                });

            svg.selectAll("text")
                .data(dataset)
                .enter()
                .append("text")
                .text(function(d) { 
                    return d;
                })
                .attr("x", function(d, i) {
                    return i * (w / dataset.length)+ (75 / dataset.length);
                })
                .attr("y", function(d) { 
                    return h - (d * 4) + 15;
                })
                .attr("font-family", "sans-serif")
                .attr("font-size", "11px")
                .attr("fill", "white");
                <%   
    } finally {
        if (stmt != null) { stmt.close(); }
    }%>
        </script>

    </body>
</html>
  • Is this a diablo 3 project, if so I'd love to help out =P – dardo May 24 '12 at 20:20
  • Sadly, no. d3.js is Data Driven Documents and is a library for creating SVG images in javascript. But when I'm working on this and I have to type d3 I cringe because I want to stop working and play! Haha. – user1410980 May 24 '12 at 20:23
  • Bah, lame. Back to topic, how heavyweight do you want to get? Most webapps these days use frameworks to get around a lot of the annoying bits of servlet architecture – dardo May 24 '12 at 20:26
  • This isn't meant to be crazy heavy. It's just for the purpose of me learning. I might try to do a test website or something after I figure everything out, but that's about it. – user1410980 May 24 '12 at 20:28

3 Answers3

2

Definitely use multiple layers to build a Web application has great benefits. The following responses will be very useful:

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

How does a JSP communicate with a servlet? By making an HTTP <form> request or an AJAX call, of course.

Your instinct is a good one: scriptlet code in a JSP is a very bad idea.

You need not jump into a framework morass righta way. There might even be value in enduring the pain of doing it without a framework. You'll appreciate one more the next time.

Figure out what you need to pass to the servlet to perform the query. Do it by POSTing a <form> to the servlet URL. The servlet will get the request parameter name/value pairs, bind them to the query, map the ResultSet into an object or collection, and add that to the page, request, or session scope for rendering. It'll also say what the next page to display should be.

Easy peasy. Good luck.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Not really. Both can be true. I'd prefer that you vote up and accept the answer. Thanks are great, but voting and accepting is good for both of us. – duffymo May 24 '12 at 20:52
  • They contradict in my mind because I generally associate good luck with some one heading out on something that will likely be difficult. I was saying that in jest because I just thought it was funny. Unfortunately, I can't upvote because I need more than 15 reputation, which I don't have. – user1410980 May 24 '12 at 20:55
  • No worries, you accepted an answer. That's what you should do. – duffymo May 24 '12 at 20:58
0

I have a simple example here: In a JSP MVC design is it possible to automatically invoke a Command upon page load?

The way to look at it, is to consider the JSP file simply as a template of what the page should look like. Wherever you have something dynamic, something that varies from page view to page view, you put in place holders. Then, in your servlet, you populate those placeholders. Once they're populated, you forward to the JSP which takes the values and put them in their proper places within the page.

Read up on JSTL and the JSP Expression Language.

Community
  • 1
  • 1
Will Hartung
  • 115,893
  • 19
  • 128
  • 203