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