Home / java / Servlet and JSP Communication

Servlet and JSP Communication

In this Article, I am going to explain how the communication happing between the servlet and JSP with a Database insert operation

ApplicaionDataSource

package com.testsevlet;

import java.sql.Connection;
import org.apache.commons.dbcp2.BasicDataSource;

public class ApplicaionDataSource {

	static BasicDataSource data=null;
	
	static {
		data=new BasicDataSource(); 
		data.setUsername("root");
		data.setPassword("");
		data.setUrl("jdbc:mysql://localhost/jdbc");
		data.setDriverClassName("com.mysql.jdbc.Driver");
		
	}
	/*ApplicaionDataSource.getConnection()*/
	 public static Connection getConnection() throws Exception{
		return data.getConnection();
	 }
	
}

TestServlet

package com.testsevlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet{
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String username  = req.getParameter("username");
		String email = req.getParameter("email");
		try{
			Connection conn  = ApplicaionDataSource.getConnection();		
			java.sql.PreparedStatement ps = conn.prepareStatement("insert into f_user (name,email)values (?,?)");
			ps.setString(1,username);
			ps.setString(2, email);
			int i = ps.executeUpdate();
			if(i>0){

				req.setAttribute("username", username);
				req.setAttribute("email", email);
				RequestDispatcher rd=req.getRequestDispatcher("done.jsp");
				rd.forward(req, resp);
						
			}else{
				RequestDispatcher rd=req.getRequestDispatcher("insert.jsp");
				rd.forward(req, resp);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  <display-name>spring<display-name>
  <welcome-file-list>
  <welcome-file>insert.jsp<welcome-file>
  <welcome-file-list>
  
  <servlet>
  <servlet-name>Test</servlet-name>
  <servlet-class>com.testsevlet.TestServlet</servlet-class>
  <servlet>
  
  <servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/test<url-pattern>
  <servlet-mapping>
<web-app>

insert.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="test" method="post">
	<input type="text" name="username">
	<input type="text" name="email">
	<input type="submit">
</form>
</body>
</html>

done.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
<title>Insert title here</title>
</head>
<body>
Username : <% String message=(String)request.getAttribute("username"); out.print(message);>
Email : <% String message1=(String)request.getAttribute("email");out.print(message1); >
</body>
</html>

About Mohammad Fareed

Software Engineer @ Tekzenit.

Check Also

Object Creation in Java

1)How many ways are there to create an object? 1. How many ways to create …

3 comments

  1. ResultSet exception – before start of result set

    • Basically, you are positioning the cursor before the first row and then requesting data. You need to move the cursor to the first row.

      result.next();
      String foundType = result.getString(1);
      It is common to do this in an if statement or loop.

      if(result.next()){
      foundType = result.getString(1);
      }

  2. What is the difference between doget,dopost and service methods in servlets.?

Leave a Reply

Your email address will not be published. Required fields are marked *