Javascript vs JSP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swethak
    New Member
    • May 2008
    • 118

    Javascript vs JSP

    Hi,

    i have the javascript and jsp combination code for calendar functionality is there.

    in that i take the events from database.For that i write a condition to select the events as "select * fom importtimetable where month='jspvar'. This statement didn't work. In that i declared jspvar as
    Code:
    <script>var jsvar=monthNum;</script>
    String jspvar="<script>document.writeln("javar")</script>";
    Now jspvar didn't pass to the select statement. Anybody plz tell that whats the wrong in that

    Code:
    [javascript]
    
    function createCalendar(monthNum,yearNum) {
    	calendarString = '';
    	var daycounter = 0;
    		
    		calendarString +='<td height=\"20\" colspan=\"4\" valign=\"top\"><a class=\"breadlink\" href=\"#\">Home</a> -  Calender of Events</td>';
    		calendarString +='<td width=\"21%\" height=\"30\"><font class=\"calender\">'+ wordMonth[monthNum-1]+ todaysDate +'</font></td>';
    		
            calendarString +='<option selected>'+ wordMonth[monthNum-1]+'</option>';
                      calendarString +='<option value=1>January</option>';
                      calendarString +='<option value=2>February</option>';
                      calendarString +='<option value=3>March</option>';
                      calendarString +='<option value=4>April</option>';
                      calendarString +='<option value=5>May</option>';
                      calendarString +='<option value=6>June</option>';
                      calendarString +='<option value=7>July</option>';
                      calendarString +='<option value=8>August</option>';
                      calendarString +='<option value=9>September</option>';
                      calendarString +='<option value=10>October</option>';
                      calendarString +='<option value=11>November</option>';
                      calendarString +='<option value=12>December</option>';
                    calendarString +='</select>';
                    calendarString +='<select name="select5">';
                      calendarString +='<option>21-24</option>';
                    calendarString +='</select>';
                    calendarString +='<select name=\"select4\" id=\"select4\" onchange=\"selectdate()\">';
                    calendarString +='<option selected>'+ yearNum +'</option>';
                     calendarString +='<option value=2006>2005</option>';
                      calendarString +='<option value=2007>2006</option>';
                      calendarString +='<option value=2008>2007</option>';
                      calendarString +='<option value=2009>2008</option>';
                      calendarString +='<option value=2010>2009</option>';
                      calendarString +='<option value=2011>2010</option>';
                      calendarString +='<option value=2012>2011</option>';
                      calendarString +='<option value=2013>2012</option>';
                      calendarString +='<option value=2014>2013</option>';
                      calendarString +='<option value=2015>2014</option>';
                      calendarString +='<option value=2016>2015</option>';
                      calendarString +='<option value=2017>2016</option>';
                    calendarString +='</select>';
            var jsvar=monthNum;
    <%@ page language="java" %>
    <%
    String connectionURL = "jdbc:mysql://localhost:3306/";
    String dbName = "user_register";
    Connection connection = null;
    Statement stmt = null;
    ResultSet rs = null;
    ResultSet rs2 = null;
    PreparedStatement pstatement = null;
    PreparedStatement pstatement2 = null;
    String title = request.getParameter("title");
    String description = request.getParameter("description");
    String month = request.getParameter("month");
    String jspvar="<script>document.writeln(jsvar)</script>";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL+dbName, "root", "root");
    pstatement2 = connection.prepareStatement("Select * from importtimetable where month='jspvar'");
    rs2 = pstatement2.executeQuery();
    while(rs2.next()){
    %>
          
            calendarString +='<td width=\"66\" height=\"66\" align=\"center\" bgcolor=\"#EBFEE0\" class=\"text\">00:00</td>';
            calendarString +='<td width=\"638\" height=\"55\" align=\"left\" valign=\"top\" bgcolor=\"#FFFFFF\"><%=rs2.getString(1)%>&nbsp;</td>';
           <% 
            }
             %>     		
    }
    
    
    [/javascript]
    Last edited by Nepomuk; Nov 28 '08, 04:23 PM. Reason: Added [CODE] tags
  • davidr69
    New Member
    • Nov 2008
    • 2

    #2
    re: Javascript vs JSP

    I see a big problem with the code. The jsvar is a variable within your browser:

    var jsvar=monthNum;

    Only your browser understands what jsvar is, but then you are trying to use it within your jsp, which basically gets compiled into a servlet and is only known to you app server (e.g. Tomcat). You cannot do this:

    String jspvar="<script >document.write ln(jsvar)</script>";

    Although jsp's can generate javascript code, javascript cannot communicate with jsp's other than invoking them via a url or AJAX.

    Basically, what you are trying to do is have javascript create your web page, but with data processed in a jsp. Don't be fooled by the jsp; it is not executed in your browser.

    I'm not sure what you are trying to accomplish. I see this:

    String month = request.getPara meter("month");

    So it looks like you are doing something like http://127.0.0.1/mycode.jsp?mont h={something}. Is "something" numeric or text? Since you are already receiving a month variable (which I don't see as being used in the jsp), you can eliminate this:

    String jspvar="<script >document.write ln(jsvar)</script>";

    That would also change your sql:

    pstatement2 = connection.prep areStatement("S elect * from importtimetable where month='" + month + "'");

    Note that the punctuation with spaces looks like this:

    where month = ' " + month + " ' "

    Don't put those spaces in your code; I just did that for readability.

    Comment

    Working...