0

I am using jsp at ubuntu machine.

Using jdbc, I could access my own database without my own function.

I tried to gather all database facilities at one jsp file and just call those functions at other jsp files.

My tries are,

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);
...

When atempting to access checkLogin.jsp, Exception error is occurred.

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=?";

I tried to search this issue, but I couldn't find proper answers. When I used Class.forName(...) at same jsp file, There is no problem. This problem was occurred when split jsp files and make a function.

Edan Choi
  • 11
  • 4
  • possible duplicate of [java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse](http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse) – yamilmedina Jun 22 '15 at 16:15
  • This is very bad practice. Make a class file. – developerwjk Jun 22 '15 at 23:25
  • @developerwjk If I don't make a class, I cannot solve this problem? Hmms.. I'm not skilled at jsp (even java). I am afraid that If I make a class, this problem is not solved. – Edan Choi Jun 23 '15 at 16:05

1 Answers1

0

Put all your functions in a class /WEB-INF/classes/packageName/className.java and compile it there. In this class you need to put package packageName; at the top and also make your functions static (if you want to be able to call them without instantiating).

Example:

package FunctionsPackage;
import java.sql.*;
public class FunctionsClass
{
    public static int CheckLogin(String login, String password)
    {
        //  query database.
        Class.forName("com.mysql.jdbc.Driver");
        String myUrl = "jdbc:mysql://localhost/prjhd";
        ....
    }
    ...
}

(You will probably also have to add some error handling to get it to compile)

Then in your JSP code reference the class by packageName.className.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<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=FunctionsPackage.FunctionsClass.CheckLogin(login, password);
...
developerwjk
  • 8,619
  • 2
  • 17
  • 33