将.java的结果输入到jsp中 [英] Get the results of .java into jsp

查看:61
本文介绍了将.java的结果输入到jsp中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做大量的JSP脚本来从数据库中调用,然后我知道这不是一个好主意,所以现在,我有点迷茫了.

I was trying to do a ton of JSP scripting to call from a DB, then I learned that it was not a good idea, so now, I'm a little lost.

FirstExample.java:

FirstExample.java:

 package p;

 //STEP 1. Import required packages
import java.sql.*;


public class FirstExample {
   // JDBC driver name and database URL
static final String JDBC_DRIVER = "oracle.jdbc.OracleDriver";  
static final String DB_URL = "url";

//  Database credentials
static final String USER = "user";
static final String PASS = "pw";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
  //STEP 2: Register JDBC driver
 Class.forName("oracle.jdbc.OracleDriver");

  //STEP 3: Open a connection
  System.out.println("Connecting to database...");
  conn = DriverManager.getConnection(DB_URL,USER,PASS);

  //STEP 4: Execute a query
  System.out.println("Creating statement...");
   stmt = conn.createStatement();
 //  String sql;
       ResultSet rs = stmt.executeQuery("some query");
System.out.println(rs);
  //STEP 5: Extract data from result set
  while(rs.next()){
     //Retrieve by column name
  //         int id  = rs.getInt("some column");
         System.out.println (rs.getString(1) + "," + rs.getString(2));
  //   int age = rs.getInt("some age");
 //    String first = rs.getString("first");
  //   String last = rs.getString("last");

     //Display values
 //      System.out.print("some column: " + id);
 //    System.out.print(",some age: " + age);
//     System.out.print(", First: " + first);
//       System.out.println(", Last: " + last);
  }
  //STEP 6: Clean-up environment
  rs.close();
  stmt.close();
  conn.close();
}catch(SQLException se){
  //Handle errors for JDBC
  se.printStackTrace();
}catch(Exception e){
  //Handle errors for Class.forName
  e.printStackTrace();
}finally{
  //finally block used to close resources
  try{
     if(stmt!=null)
        stmt.close();
  }catch(SQLException se2){
  }// nothing we can do
  try{
     if(conn!=null)
        conn.close();
  }catch(SQLException se){
     se.printStackTrace();
  }//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample

我需要将其结果输入到JDBCQuery2.jsp中,其内容为:

I need to feed the results of this into JDBCQuery2.jsp, which reads:

<%@ page language="java" import="p.FirstExample" %>
<% FirstExample object = new FirstExample();


//FirstExample.init();    

%>
<h3>Connection ok</h3>

这可以正确地加载包,但是我需要.java中方法的结果,而且我不知道如何使用上面的init调用该方法.我应该使用init吗?最终,我想将Java引用与ajax调用相关联,但是现在任何人都可以帮助我将查询结果拖入我的JSP中吗?任何帮助将不胜感激

This properly loads the package, but I need the results of the method in the .java and I have no idea how to call the method using the init above. Should I be using init? Eventually, I would like to associate the java reference to an ajax call, but for now could anyone help me pull the query results into my JSP? Any help would be greatly appreciated

推荐答案

从JSP调用Java main()方法不是JSP的工作方式.

Calling a Java main() method from your JSP isn't the way JSP works.

通常,对于基本的纯JSP解决方案,您要做的是将连接信息放置在web.xml中.然后,编写一个扩展HttpServlet类的servlet,并在您为该类重写的init()方法中从ServletContext访问该信息.一旦您的servlet被容器加载,就会发生这种初始化.之后,对该Servlet的服务方法(例如doGet())的调用可以重定向到您的JSP.然后,您可以通过多种方式访问​​连接信息.例如,通过Java scriptlet访问ServletContext属性或通过EL访问applicationScope.

In general, for a basic pure JSP solution, what you want to do is place the connection information in the web.xml. Then you write a servlet that extends the HttpServlet class and accesses that information from the ServletContext in the init() method which you override for this class. Once your servlet is loaded by the container, this initialization occurs. After that, a call on this servlet's service method (e.g. doGet()) can redirect to your JSP. You can then access the connection information in a variety of ways; for example, by accessing the ServletContext attributes via Java scriptlet or the applicationScope via EL.

听起来很复杂,但这是基本的JSP/Servlet机制.您的问题似乎表明您是JSP的新手,可能需要通过a来运行本教程.

It sounds complicated but it's basic JSP/Servlet mechanism. You question seems to indicate that you're new to JSP and might need a to run through a tutorial.

根据您的环境和您要尝试执行的操作,有更复杂的方法.

There are more sophisticated ways of doing this depending on your environment and what you are trying to do.

这篇关于将.java的结果输入到jsp中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆