1

I need your help.
I have to develop a Web Application which consists in a JSP and a Java Bean. The JSP file has to get two parameters (name and account) and then insert them in a database only using bean and jsp tags. I started to do something:
Java Bean:

    package beans;
    public class java1 {
        private String name = "";
        private String account = "";

        public String getName(){
            return name;
        }

        public String getAccount(){
            return account;
        }

        public void setName(String name){
            this.name = name;
        }

        public void setAccount(String account){
            this.account = account;
        }
    }

JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <%@page import = "java.sql.*"%>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%String nome = (String) request.getParameter("nome");
        String account = (String) request.getParameter("account");%>
        <jsp:useBean id = "esame" class = "beans.java1" scope = "page"/>
            <jsp:setProperty name = "esame" property = "name" value = "<%=nome%>"/>
            <jsp:setProperty name = "esame" property = "account" value = "<%=account%>"/>
    </head>
    <body>
        <h1>Nome: </h1>
        <jsp:getProperty name = "esame" property = "name"/>
        <h1>Account:</h1>
        <jsp:getProperty name = "esame" property = "account"/>

    </body>
</html>

Now the Java Bean and jsp page itself work good. I need to know how to get those two parameters and insert them in database. Table structure would be:

users(name, account);

I know how to insert data into a database using JDBC driver, here's an example method:

public static void insert(String name, String account){
        String sql = "insert into users values('" + name + "', '" + account + "');";
        String url = "jdbc:postgresql://localhost/tests";
        Connection conn = null;
        Statement st = null;
        try{
            conn = DriverManager.getConnection(url);
            st = conn.createStatement();
            st.executeUpdate(sql);
            st.close();
            conn.close();
        }
        catch(SQLException ecc){
            System.out.println(ecc.getMessage());
        }
    }

But how can I do it using ONLY java bean and jsp tags? Sorry for the long question, I only wanted to be clear. Thank you

1
  • There is no way to persist data using only beans and JSP tags. What you can do (but shouldn't) is insert your JDBC code inside your JSP tags. You should use at least a servlet to hold persistence logic. Commented Jun 11, 2018 at 5:01

1 Answer 1

1

If you ONLY want beans and jsp's (which is really bad) then you can just put the insert method in the java1 class and then call it from the jsp like

<%    esame.insert(); %>

Of course you will need a second jsp with a form for inserting the data. If the user submits then you call a third jsp that calls the insert method.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not an Application I'd use, it's just an exercise. By the way I did it your way and it works. I also created a new property in class bean1 which inserts the other's two properties in DB by calling the setProperty tag in jsp. Works this way too. Thank you
Thanx. If you ONLY want beans and jsp's : If not, what would be the suggestion, how can a bean be mapped to rows of the DB?
Then you should do the insert (or any other DML operation) in a separate Java class (not in the JSP). The jsp is for display only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.