File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How do I solve the problem:Syntax error on tokens, delete these tokens? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How do I solve the problem:Syntax error on tokens, delete these tokens?" Watch "How do I solve the problem:Syntax error on tokens, delete these tokens?" New topic
Author

How do I solve the problem:Syntax error on tokens, delete these tokens?

Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
I need help to solve the errors that I am getting for my connection pooling code:-

Generated servlet error:
Syntax error on token "import", delete this token


Generated servlet error:
Syntax error on tokens, delete these tokens


Please do help, thanks!

THE CODE:

<%@ page import="javax.naming.*,java.sql.*,java.io.*,java.lang.*,java.text.*,java.util.*"%>
<%
public class ConnectionPool implements Runnable
{
private int m_InitialConnectionCount = 5; // Number of initial connections to make.
private Vector m_AvailableConnections = new Vector();// A list of available connections for use.
private Vector m_UsedConnections = new Vector();// A list of connections being used currently.
private String m_URLString = null;// The URL string used to connect to the database
private String m_UserName = null; // The username used to connect to the database
private String m_Password = null;// The password used to connect to the database
private Thread m_CleanupThread = null;// The cleanup thread

//Constructor
public ConnectionPool(String urlString, String user, String passwd) throws SQLException
{
// Initialize the required parameters
m_URLString = urlString;
m_UserName = user;
m_Password = passwd;

for(int cnt=0; cnt<m_InitialConnectionCount; cnt++)
{
m_AvailableConnections.addElement(getConnection());// Add a new connection to the available list.
}

// Create the cleanup thread
m_CleanupThread = new Thread(this);
m_CleanupThread.start();
}

private Connection getConnection() throws SQLException
{
return DriverManager.getConnection(m_URLString, m_UserName, m_Password);
}

public synchronized Connection checkout() throws SQLException
{
Connection newConnxn = null;

if(m_AvailableConnections.size() == 0)
{

newConnxn = getConnection();// Im out of connections. Create one more.
m_UsedConnections.addElement(newConnxn);// Add this connection to the "Used" list.
// We dont have to do anything else since this is
// a new connection.
}
else
{
// Connections exist !

newConnxn = (Connection)m_AvailableConnections.lastElement();// Get a connection object
m_AvailableConnections.removeElement(newConnxn);// Remove it from the available list.
m_UsedConnections.addElement(newConnxn); // Add it to the used list.
}

return newConnxn;// Either way, we should have a connection object now.
}

public synchronized void checkin(Connection c)
{
if(c != null)
{
m_UsedConnections.removeElement(c);// Remove from used list.
m_AvailableConnections.addElement(c); // Add to the available list
}
}

public int availableCount()
{
return m_AvailableConnections.size();
}

public void run()
{
try
{
while(true)
{
synchronized(this)
{
while(m_AvailableConnections.size() > m_InitialConnectionCount)
{
// Clean up extra available connections.
Connection c = (Connection)m_AvailableConnections.lastElement();
m_AvailableConnections.removeElement(c);

c.close(); // Close the connection to the database.
}

// Clean up is done
}

out.print("CLEANUP : Available Connections : " + availableCount());

// Now sleep for 1 minute
Thread.sleep(60000 * 1);
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


// Example code using ConnectionPool.

import java.sql.*;

public class Main
{
public static void main (String[] args)
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E)
{
out.print("Unable to load driver.");
E.printStackTrace();
}

try
{
ConnectionPool cp = new ConnectionPool("jdbc:mysql://localhost/publications", "books", "books");

Connection []connArr = new Connection[7];

for(int i=0; i<connArr.length;i++)
{
connArr[i] = cp.checkout();
out.print("Checking out..." + connArr[i]);
out.print("Available Connections ... " + cp.availableCount());
}

for(int i=0; i><connArr.length;i++)
{
cp.checkin(connArr[i]);
out.print("Checked in..." + connArr[i]);
out.print("Available Connections ... " + cp.availableCount());
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
%>
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Problem with import statement,



There are some syntactically wrong with this import tag, How can you import more than one package in JSP page ?

I know the answer but I want you to find out


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
Thanks, I just tried the below :-

import= javax.naming.*;
import = java.sql.*;
import = java.io.*;
import = java.lang.*;
import = java.text.*;
import = java.util.*;

... and I got more errors. Can you please give me another hint coz i'm totally lost
The errors:-

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on token "import", invalid Expression

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on tokens, SimpleName expected instead

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on tokens, SimpleName expected instead

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on tokens, SimpleName expected instead

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on tokens, SimpleName expected instead

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on tokens, SimpleName expected instead

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on token "*", Identifier expected

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on token "import", delete this token

An error occurred at line: 1 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Syntax error on tokens, delete these tokens

Please hheelllpp...
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Nothing wrong with your import statement, in jsp page you can always have multiple imports. But something looks wrong with you class declarations, in a jsp page we can only declare abstract or final classes. Another mistake is here.

// Example code using ConnectionPool.

import java.sql.*;

In a jsp page you can not use the import statement any where, it has to come under page directive typically the first statement.

Moreover I am not being able to make out what exactly you want to do, why so many class declarations in side a jsp page?


Swastik
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ok, I think the problem is in this import , but there is one more import statement in the code, which I haven't seen previously,



Remove this import and re run !!
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

I was bit wrong in my post i.e. "in a jsp page we can only declare abstract or final classes". We can declare classes with abstract , final or no access modifier, but public is not allowed.
Nina Savannah
Ranch Hand

Joined: Oct 15, 2008
Posts: 35
Thanks guys...I have just configured settings in server.xml and web.xml for connection pooling. Now in this code I am attempting to apply
the connection pooling to my web system. I am not sure how right or wrong I am here.

I have removed

import java.sql.*; // REMOVE THIS IMPORT

and below are the errors I am now getting.


An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Illegal modifier for the local class ConnectionPool; only one of abstract or final is permitted

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Illegal modifier for the local class Main; only one of abstract or final is permitted

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
The method main cannot be declared static; static methods can only be declared in a static or top level type

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method

An error occurred at line: 2 in the jsp file: /publications/connpool.jsp
Generated servlet error:
Cannot refer to a non-final variable out inside an inner class defined in a different method

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Nina,
Use DAO class to communicate with DB . and writing the java code into jsp is not a good parctise .jsp is used for presentation logic not for bussiness logic
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

As said above its better to use jsp just for the presentation logic, but in case you declare classes in jsp page it can't have public/private or protected access modifiers.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You should be writing Java code in Java classes.
It will also make this kind of problems disappear.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I solve the problem:Syntax error on tokens, delete these tokens?