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

[java]
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();
}

}

[/java]

TestServlet

[java]
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();
}
}

}
[/java]

web.xml

[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>
[/xml]

insert.jsp

[java]
<%@ 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>
[/java]

done.jsp

[java]
<%@ 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>
[/java]

3 Comments

Add a Comment

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