Home Tutorial Servlet Invoke JavaScript function from servlet

 
 

Share on Google+Share on Google+
Invoke JavaScript function from servlet
Posted on: July 15, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to call JavaScript function from the servlet.

Invoke JavaScript function from servlet

You have learnt several servlet related examples. Here we are going to create an example that will call JavaScript function from the servlet. Embedding JavaScript in Servlets can be useful when migrating Web applications that rely on JavaScript to server-side Java without rewriting the business logic.

Description Of Code:

You can see in the given code, we have retrieved database table data into the html tables. We have also embedded delete button in every row of html table to allow the user to select the particular record to delete. This delete button calls the javascript function for the particular record. Before deleting, it will ask the user whether he/she really want to delete the record. If ok, then it will take the id of the specific record and navigate to delteuser.jsp that will delete the record.

Here is the code of Servlet.java:

import java.io.*;
import
java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
out.println(
"<html><head><script>function deleteRecord(id){var r=confirm('Are you confirm')");
out.println(
"if (r==true){var reason=prompt('Reason for deleting:')");
out.println(
"if(reason!=null){");
out.println(
"window.open('http://localhost:8080/examples/modified/deleteuser.jsp?id='+id+'&&reas='+reason,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes')");
out.println(
"}else{}}}</script></head>");
out.println(
"<form name='form'>");
out.println(
"<table border='1'>");
out.println(
"<tr><th>FirstName</th><th>LastName</th><th>Address</th><th>Email</th></tr>");
Connection con =
null;
String url =
"jdbc:mysql://localhost:3306/";
String db =
"test";
String driver =
"com.mysql.jdbc.Driver";
String userName =
"root";
String password =
"root";
int sumcount = 0;
Statement st;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url + db, userName, password);
String query =
"select * from employee";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
out.println(
"<tr><td>" + rs.getString(2) + "</td><td>"+ rs.getString(3) + "</td><td>" + rs.getString(4)+ "</td><td>" + rs.getString(5) + "</td>");
out.println(
"<td><input type='button' name='edit' value='Delete' style='background-color:red;font-weight:bold;color:#ffffff;' onclick=\"deleteRecord('"+ rs.getString(1) + "');\"></td></tr>");
}
}
catch (Exception e) {
e.printStackTrace();}
out.println(
"</table>");
out.println(
"<form>");
out.println(
"</html>");
}
}

Here is the code of deleteuser.jsp:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
int
no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from register where id='"+no+"'";
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM register WHERE id = '"+no+"'");
out.println("Record is deleted successfully");
}
catch(Exception e){}
%>

Advertisement

Related Tags for Invoke JavaScript function from servlet:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com