• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
}
%>
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Nina Savannah
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be writing Java code in Java classes.
It will also make this kind of problems disappear.
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic