Create a new Web application. Next, create a JSP that displays a form (with 3 us
ID: 664242 • Letter: C
Question
Create a new Web application. Next, create a JSP that displays a form (with 3 user inputs) when the doGet method is invoked. The form will contain a post action that directs the form post back to the same JSP, which in the doPost method will save the form data to a database using a Java Bean and a Custom Tag. Use Oracle and SQL to make the DB connection. After the form data has been saved to the database, respond back with a query from the database displaying all the current records contained in the database.
Explanation / Answer
given jsp,bean, servlet,dbconnection,dao files.....this format is MVC architecture
<%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<html>
<head><h1>Filling Form</h1><head>
<body>
<form name="myForm" action="insert" method="post">
<input type="text" placeholder="nmae" name="name">
<input type="text" placeholder="designation" name="designation">
<input type="text" placeholder="country" name="country">
<br>
<button class="btn-primary" type="submit">Save Details</button>
</form>
<%
Connection con=null;
String url = "jdbc:mysql://localhost:3306/mysql?user=root&password=root";
try {
Class.forName("com.mysql.jdbc.Driver");
// Get a connection
con = DriverManager.getConnection(url);
} catch (Exception except) {
except.printStackTrace();
}
Statement statement = con.createStatement();
ResultSet resultset =
statement.executeQuery("select * from myData") ;
if(!resultset.next()) {
out.println("Data Empty");
} else {
%>
<TABLE BORDER="1">
<TR>
<TH>Name</TH>
<TH>Designation</TH>
<TH>Country</TH>
</TR>
<TR>
<TD> <%= resultset.getString(1) %> </TD>
<TD> <%= resultset.getString(2) %> </TD>
<TD> <%= resultset.getString(3) %> </TD>
</TR>
</TABLE>
<BR>
<%
}
%>
</body>
<html>
-------------------------------------------------------------
public class saveBean {
//bean clss contains getter setter methods
private String name;
private String designation;
private String country;
public String getname()
{
return name;
}
public void setname(String name)
{
this.name=name;
}
public String getdesignation(){
return designation;
}
public void setdesignation(String designation)
{
this.designation=designation;
}
public String getcountry(){
return country;
}
public void setRole_name(String country)
{
this.country=country;
}
}
----------------------------------------------------------------------------------
my db connection
-----------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DBConnect1 {//savingmy db connection on one file
public static Connection getConnection()
{
Connection con=null;
String url = "jdbc:mysql://localhost:3306/mysql?user=root&password=root";
try {
Class.forName("com.mysql.jdbc.Driver");
// Get a connection
con = DriverManager.getConnection(url);
} catch (Exception except) {
except.printStackTrace();
}
return con;
}
}
--------------------------------------------------------------------------------------
my dao file
----------------------------------------------
import db.DbConnect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class insertDataDao {
public void DatabaseInsertion(String name,String designation,int country)
{
Connection con=null;
Statement stmt=null;
try{//opening conncetion
con = DbConnect.getConnection();
stmt=con.createStatement();
catch(SQLException e){
e.printStackTrace();
}
String values = "('"+name+"','"+designation+"','"+country+"','"+join_date+"','"+end_date+"','"+client_id+"')"; //inserting into dtaabase
String query = "insert into myData(name,designation,country) values "+values;
int rowsEffected = stmt.executeUpdate(query);
System.out.println("Number of rows effected by the first insert command are:"+rowsEffected);
con.close();
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
}
}
------------------------------------------------------------------------
servlet file
--------------------------------------------------------------
import bean.saveBean;
import dao.insertDataDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
@WebServlet(urlPatterns = {"/saveservlet"})
public class loginservlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
String designation=request.getParameter("designation");
String country=request.getParameter("country");
HttpSession ss=request.getSession(true);//adding data to session
ss.setAttribute("name",name);
ss.setAttribute("designation",designation);
ss.setAttribute("country",country);
try {
saveBean user = new saveBean();//setting data to bean
user.setname(request.getParameter("name"));
user.setdesignation(request.getParameter("designation"));
user.setcountry(request.getParameter("country"));
user = insertDataDao.DatabaseInsertion(name,designation,country); //call class to DAO filr
response.sendRedirect("./Pages/myPage.jsp"); //initial page
}
finally {
out.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.