January 14, 2010

Student Application Java web application using Servlet Mysql JSP - Athul kannoth


How the request goes ?

The first page executed will be  welcome file in web.xml  then to JSP page , the user click on submit button particular servlet will be called which is specified in the action tag. The request created (example shown by ***) will be reached at web.xml, where we specified servlet mapping. thus it identifies which servlet will be called. since we uses get method, request reaches doGet method inside the servlet.

After getting request parameter using request.getparameter, we assign the values to student pojo object ( to pass the value to insert query inside dao).

Once the request reach dao, we create a connection object for connecting to mysql database, where we need to specifies vendor name, database location, database name, and credential to access the database.

Sql queries are exceuted by Java ( java data base connectivity - JDBC) using a preparestatement.

Thus insert the value to database. Once inserted we will " successfull inserted " message to Users

For that we use the following method inside servlet

response.getWriter().append(message).append(request.getContextPath());

this is the response to user ( ie response will be send back to jsp -java server page)




0. Welcome file (web.xml)

 <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

List of welcome file to be mentioned in the studentapp. which is the entry page of the application.


1. Inside JSP page (file Name : index.jsp)

<form action="StudentServlet">

Please enter the name of Student <input type="text" name="name" /><br>
Please enter the roll NO of Student <input type="text" name="rollNo" /><br>
Please enter the age of Student <input type="text" name="age" /><br>
Please enter the class of Student <input type="text" name="stream" /><br>
<input type="submit" value="submit">

</form>

This is called form action. Once user submit a button  the request will be send to the particular servlet mention in the form action.

*** Request : http://localhost:8080/StudentApp/StudentServlet?name="athul"&age="25"&stream="tcs"&rollno="1224"


2. Inside deployment descriptor ( web.xml)

<servlet>
    <servlet-name>StudentServlet</servlet-name>
    <servlet-class>com.konzern.studentapp.servlets.StudentServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>StudentServlet</servlet-name>
    <url-pattern>/StudentServlet</url-pattern>
  </servlet-mapping>

This is called servlet mapping - mapping a particular url (http request) to a servlet.





StudentServlet ( Servlet class)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/
Student student = new Student();
student.setName(request.getParameter("name"));
student.setRollNo(Integer.parseInt(request.getParameter("rollNo")));
student.setAge(Integer.parseInt(request.getParameter("age")));
student.setStream(request.getParameter("stream"));

StudentDAOservice = new StudentDAO();
String message = service.insert(student);

response.getWriter().append(message).append(request.getContextPath());
}

Once the http request reaches the servlet, the parameters in the http url are get using request.getparameter("name");

The parameters are encapsulated to a student POJO, to data transfer the data inside the application
here in our case to DAO packages.

Student Object will be send to StudentDAO insert method.

Student ( POJO Class)

/**
 *
 */
package com.konzern.studentapp.model;

/**
 * @author Gokul Balan
 *
 */
public class Student {

private String name =null;
private int rollNo = 0;
private int age = 0;
private String stream= null;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
}



StudentDAO.java ( Data access Objects)

public String insert(Student student) throws SQLException {
PreparedStatement p= MySQLConnection.prepareStatement(" insert into ipmanagetable(name,age,rollno,class) values(?,?,?,?)");
p.setString(1,student.getName());
p.setInt(2,student.getAge());
p.setInt(3, student.getRollNo());
p.setString(4, student.getStream());
int output = p.executeUpdate();
return "Student information saved";
}


Here is where we use to write queries to insert, update, delete operations ( SQL queries ).

We use PrepareStatement in JDBC to write sql queries.

PreparedStatement.execute(); method will exceute the sql query.


MySQL Connection class


public static Connection connectionInstance() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =  DriverManager.getConnection("jdbc:mysql://localhost/StudentAppDb", "username", "password");
return con;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

You need to specify which driver you are using
Class.forName("com.mysql.jdbc.Driver");

For different database vendors the driver names will be different .

com.mysql.jdbc.Driver is for mysql.

In DriverManager.getConnection, you need to specify database vendor:// location of database/ database name in additional to authentication credentials ie username & password.



Thanks
Athul Kannoth


For any queries feel free to contact us.
















No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments