Java 在jsp中添加一个类

Java 在jsp中添加一个类,java,apache,class,jsp,tomcat,Java,Apache,Class,Jsp,Tomcat,我在一个JSP页面中工作,该页面如下所示: <%@page import="java.util.*" %> <% String[] colors = {"Gray", "Brown", "Red" , "Orange", "yellow", "Green", "Blue", "purple"}; String color, numero; color=request.getParameter("colores"); numero=request.getParam

我在一个JSP页面中工作,该页面如下所示:

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


<%
 String[] colors = {"Gray", "Brown", "Red" , "Orange", "yellow", "Green", "Blue", "purple"};
 String color, numero;

 color=request.getParameter("colores");
 numero=request.getParameter("numeros");

 int c = Integer.parseInt(request.getParameter("colores")); 
 int num = Integer.parseInt(request.getParameter("numeros"));

%>

<HTML>


  <BODY>  

    <TABLE BORDER="1" align="center" bgcolor="#E8FDFF" height="40%">

     <TR>
     <TD bgcolor="<%=colors[c-1] %>"> 


    <% HERE GOES THE JAVA PART %>


     </TD>
     </TR>


    </TABLE>

  </BODY>

</HTML>
public class Tabla 
{
public static void main (String[] args) 
    {        
        int n=67;       
        int j;

        Tabla table = new Tabla ();
        int dato[];
        dato=table.producto(n);        

        for (j=0;j<10;j++)
        {System.out.println(dato[j]);
        }

    }



    public int [] producto(int num) 
    { 
        // make a 10-element array
        int a[] = new int[10];

       // fill up the array with products
        for (int i = 0; i < 10; i++)
        {a[i] = num * (i+1); }

        return a;        

    }    


}

我想添加一个java类和方法,如下所示:

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


<%
 String[] colors = {"Gray", "Brown", "Red" , "Orange", "yellow", "Green", "Blue", "purple"};
 String color, numero;

 color=request.getParameter("colores");
 numero=request.getParameter("numeros");

 int c = Integer.parseInt(request.getParameter("colores")); 
 int num = Integer.parseInt(request.getParameter("numeros"));

%>

<HTML>


  <BODY>  

    <TABLE BORDER="1" align="center" bgcolor="#E8FDFF" height="40%">

     <TR>
     <TD bgcolor="<%=colors[c-1] %>"> 


    <% HERE GOES THE JAVA PART %>


     </TD>
     </TR>


    </TABLE>

  </BODY>

</HTML>
public class Tabla 
{
public static void main (String[] args) 
    {        
        int n=67;       
        int j;

        Tabla table = new Tabla ();
        int dato[];
        dato=table.producto(n);        

        for (j=0;j<10;j++)
        {System.out.println(dato[j]);
        }

    }



    public int [] producto(int num) 
    { 
        // make a 10-element array
        int a[] = new int[10];

       // fill up the array with products
        for (int i = 0; i < 10; i++)
        {a[i] = num * (i+1); }

        return a;        

    }    


}
公共类Tabla
{
公共静态void main(字符串[]args)
{        
int n=67;
int j;
Tabla table=新Tabla();
国际数据[];
dato=表.产品(n);
对于(j=0;j而言,您不能在web应用程序中使用
main()
方法,因此在jsp页面中使用
main()
方法将您的类放置在jsp页面中是不幸运的。另外,您可以尝试以下两种方法之一:

  • 编写一个简单的bean类,以公共方法的形式将代码放在那里,在jsp中导入该类,并通过使用
    将属性集标记到该类对象来创建该类对象,然后根据需要在该对象上调用方法
  • 创建一个只包含应用程序某些视图部分的jsp页面,在jsp中使用
    tag
    ,使用一个servlet并将其与url模式映射,使用表单标记中此servlet的url作为操作值,如下所示将控件发送到servlet,将逻辑放入servlet的
    doGet(-)
    doPost(-)
    执行逻辑的方法

我查了一下,我的问题是如何将java类添加到jsp页面?使用第一个解决方案(编写一个简单的bean类),您如何将该类导入jsp的项目文件夹?您只是简单地将Netbeans生成的类文件拖到jsp项目文件夹中吗?谢谢,不,1)将您的bean类放入一个包中(不需要修改,但推荐)在项目的classes文件夹中。2)现在要导入该类,请在jsp页面顶部写入3)导入该类后创建该类的对象使用..有关更多信息,请参阅此处: