0

Hey guys I have a java class which uses a microsoft access database to validate login details which works successfully. I cannot however manage use this java class for use with my jsp page. I have tried the following. Any help appreciated

login.jsp

<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="login" %>   

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Angels & Demons</title>
        <a href="index.jsp">Home Page</a>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <h1><center>Login</center></h1>
        <center>
            <h2>Please make sure to fill all fields! </h2> 
            <table> 
                <tr><td>User:<input name="name" type="text" size="10"></td></tr> 
                <tr><td>Password:<input name="password" size="10"></td></tr> 
                <td><input type="submit" value="Submit"></input></td>
            </table>   
        </center>
    </body>
</html>

login.java

import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class login {

    Connection con;
    Statement st;
    ResultSet rs;

    JFrame f = new JFrame("User Login");
    JLabel l = new JLabel("Username:");
    JLabel l1 = new JLabel("Password:");
    JTextField t = new JTextField(10);
    JTextField t1 = new JTextField(10);
    JButton b = new JButton("login");

    public login(){
        connect();
        frame();
    }

    public void connect(){
        try{

            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
            Class.forName(driver);

            String db = "jdbc:odbc:AngelsAndDemons";
            con = DriverManager.getConnection(db);
            st = con.createStatement();
        } catch(Exception ex) {
        }
    }

    private void frame() {
       f.setSize(600,400);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setVisible(true);

       JPanel p = new JPanel();
       p.add(l);
       p.add(t);
       p.add(l1);
       p.add(t1);
       p.add(b);

       f.add(p);

       b.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e) {                   
               try{
                   String user = t.getText().trim();
                   String pass = t1.getText().trim();

                   String sql = "select user,pass from AngelsAndDemons where user = '"+user+"'and pass = '"+pass+"'";
                   rs = st.executeQuery(sql);

                   int count = 0;
                   while(rs.next()) {
                       count = count + 1;
                   }

                   if(count == 1) {
                       JOptionPane.showMessageDialog(null,"User found, Access Granted!");
                   } else if(count > 1){
                       JOptionPane.showMessageDialog(null,"Duplicte User, Access Denied");
                   } else{
                       JOptionPane.showMessageDialog(null,"User not found");
                   }
               } catch(Exception ex) {
               }
           }
       });
    }

    public static void main(String[] args){
        new login();
    }
}//END CLASS
OO7
  • 2,785
  • 1
  • 21
  • 33
  • 1
    You need a form inside your .jsp (around inputs) which send data to a servlet. This servlet will use another class to connect to the database... Your login class should not use JFrame and other swing components – Happy Jan 24 '14 at 18:21
  • Ive no idea how to use a servlet –  Jan 24 '14 at 18:26
  • @Happy He certainly needs a form, but a JSP *is* a servlet. – user207421 Jan 24 '14 at 20:16
  • Can you help me out here? –  Jan 24 '14 at 20:20
  • possible duplicate of [Java Login JSP Page (Uses Access Database)](http://stackoverflow.com/questions/21338715/java-login-jsp-page-uses-access-database) – Elliott Frisch Jan 24 '14 at 20:26

2 Answers2

0

In order to use java code inside jsp page you must use this <% %> here is an example

fnkbz
  • 1,189
  • 1
  • 12
  • 22
0
login.java

 package test.example;

    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;



    public class login {
    ....
    }

in login.jsp

<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="test.example.*" %>   

After making these changes clean your project, rebuild. it should work.

java seeker
  • 1,246
  • 10
  • 13
  • Still the same, I'm at it with hours and still no luck, any suggestions? Thanks very much for your contribution also –  Jan 24 '14 at 19:30
  • @Alexey - Any recommendations as to how I can do it so? –  Jan 24 '14 at 19:31
  • @Alexey thanks but I have no idea how to do it - fairly new to the whole JSP thing, under pressure for a project deadline. As I said, I have a working java class with microsoft access, All i want to do is acheive the same functionality within JSP , as the project I am doing is a web application, but just struggling –  Jan 24 '14 at 19:47
  • i did not notice swing component.sorry it shall no work in servlet. – java seeker Jan 24 '14 at 19:49