【发布时间】:2015-06-22 15:52:31
【问题描述】:
我在 ubuntu 机器上使用 jsp。
使用 jdbc,我可以访问自己的数据库,而无需自己的功能。
我试图在一个 jsp 文件中收集所有数据库设施,然后在其他 jsp 文件中调用这些函数。
我的尝试是,
api/Login.jsp
<%@page import="java.sql.*"%>
<%!
public int CheckLogin(String login, String password)
{
// query database.
Class.forName("com.mysql.jdbc.Driver");
String myUrl = "jdbc:mysql://localhost/prjhd";
...
checkLogin.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="api/Login.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hyundai co-relation page</title>
</head>
<body>
<%
// get parameters.
String login=request.getParameter("login");
String password=request.getParameter("password");
int result=CheckLogin(login, password);
...
尝试访问checkLogin.jsp时,出现Exception错误。
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 6 in the jsp file: /api/Login.jsp
Unhandled exception type ClassNotFoundException
3: public int CheckLogin(String login, String password)
4: {
5: // query database.
6: Class.forName("com.mysql.jdbc.Driver");
7: String myUrl = "jdbc:mysql://localhost/prjhd";
8: Connection conn = DriverManager.getConnection(myUrl,"prjhd","---");
9: String query = "select password from User where login=?";
我试图搜索这个问题,但我找不到正确的答案。 当我在同一个 jsp 文件中使用 Class.forName(...) 时,没有问题。 这个问题是在分割jsp文件和make函数的时候出现的。
【问题讨论】:
-
这是非常糟糕的做法。制作一个类文件。
-
@developerwjk 如果我不上课,我不能解决这个问题吗?嗯..我不擅长jsp(甚至java)。恐怕我做个班,这个问题不解决。