Simply put <%....%> around any class, transform to JSP?

I am trying to produce some dynamic tables and put them on the web. I simply put <%%> around an application file to test if it work, but does not.

Please tell me how can I get it work ? or no possible at all?

Your help is greatly appreciated.

Joe zhang

<%@ page language="java" %>

<%@ page import="java.io.*" %>

<%@ page contentType = "text/html" %>

<HTML>

<HEAD>

<TITLE> Add New Location Information</TITLE>

</HEAD>

<BODY bgcolor="FFF5D0">

<%

import javax.swing.JTable;

import javax.swing.JScrollPane;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.awt.*;

import java.awt.event.*;

public class SimpleTableDemo extends JFrame {

private boolean DEBUG = true;

public SimpleTableDemo() {

super("SimpleTableDemo");

Object[][] data =

{

{"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},

{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},

{"Kathy", "Walrath","Chasing toddlers", new Integer(2), new Boolean(false)},

{"Mark", "Andrews","Speed reading", new Integer(20), new Boolean(true)},

{"Angela", "Lih","Teaching high school", new Integer(4), new Boolean(false)}

};

String[] columnNames =

{"First Name", "Last Name","Sport","# of Years","Vegetarian"};

final JTable table = new JTable(data, columnNames);

table.setPreferredScrollableViewportSize(new Dimension(500, 70));

if (DEBUG) {

table.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

printDebugData(table);

}

});

}

JScrollPane scrollPane = new JScrollPane(table);

getContentPane().add(scrollPane, BorderLayout.CENTER);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

private void printDebugData(JTable table) {

int numRows = table.getRowCount();

int numCols = table.getColumnCount();

javax.swing.table.TableModel model = table.getModel();

System.out.println("Value of data: ");

for (int i=0; i < numRows; i++) {

System.out.print("row " + i + ":");

for (int j=0; j < numCols; j++) {

System.out.print(" " + model.getValueAt(i, j));

}

System.out.println();

}

System.out.println("--");

}

public static void main(String[] args) {

SimpleTableDemo frame = new SimpleTableDemo();

frame.pack();

frame.setVisible(true);

}

}

%>

</BODY>

</HTML>

[2791 byte] By [zjq81096] at [2007-9-26 23:43:54]
# 1

The code you listed won't work. The reason is because you are making the assumption that <% %> creates an empty .java file that you can just put code into. The <% %> is part of a larger .java file, so you can't put import statements in the middle of the code in that manner.

Your JSP page will be compiled into a servlet that generally looks like this:

import javax.http.*;

import ...

public class JspPageName extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse response) throws IoException... {

import javax.swing.JTable;

import javax.swing.JScrollPane;

import javax.swing.JPanel;

import javax.swing.JFrame;

import java.awt.*;

import java.awt.event.*;

public class SimpleTableDemo extends JFrame {...

}

}

I know the above code is hard to read due to formatting issues, but it illustrates that you are creating classes within classes. Don't forget that your code is PART of a larger class. The best thing to do is to look at the .java file that is generated by your container and it will be fairly obvious why your code won't work.

Michael

mjacobsca at 2007-7-4 13:44:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...