Reputation: 25
I am new to java programming and I am trying to pass a value from servlet to javascript in jsp using struts.
servlet class-
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
public class handler extends org.apache.struts.actions.DispatchAction{
private static final String SUCCESS = "success";
public ActionForward Add(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
actionFileAndDB obj=new actionFileAndDB();
JSONArray list=obj.view();
request.setAttribute("jsonArray",list);
System.out.println("this is being called");
String s="Karthikeyan";
request.setAttribute("myname",s);
return mapping.findForward(SUCCESS);
}
}
jsp-
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View</title>
</head>
<body>
<form action="operations.do">
<table>
<tr>
<td>
<select value="s" name="dropdown">
<option>VIEW</option>
<option>Database</option>
<option>File</option>
<option>Both</option>
</select>
</td>
<td>
<input type="button" value="Add" name="actionMethod" onclick="javascript:viewData();" />
</td>
<td>
<input type="submit" value="Edit" name="actionMethod"/>
</td>
<td>
<input type="submit" value="Delete" name="actionMethod"/>
</td>
</tr>
</table>
</form>
<p id="p1">
</p>
<script type="text/javascript">
function viewData()
{
var c="Hello";
document.getElementById("p1").innerHTML=c;
//JSONArray j=request.getAttribute("jsonArray");
//document.getElementById("p1").innerHTML=j;
//String s=request.getAttribute("myname");
//document.getElementById("p1").innerHTML=s;
event.preventDefault();
}
</script>
</body>
</html>
When I uncomment the getAttribute("myname"), nothing gets displayed from the paragraph tag.I wanted to know if there is any way to retrieve the value from servlet to javascript without using JQuery or Ajax for the time being.
//edited with the complete code shown-
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>view.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action path="/view" forward="/view.jsp"/>
<action path="/operations" type="handler" parameter="actionMethod">
<forward name="success" path="/view.jsp"/>
</action>
</action-mappings>
</struts-config>
view.jsp and handler.java is already shown
actionFileAndDB.java
import java.io.*;
import java.lang.*;
import java.sql.*;
import java.util.*;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
public class actionFileAndDB{
public actionFileAndDB(){}
public JSONArray view() throws ClassNotFoundException{
JSONArray list = new JSONArray();
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM employee order by id";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("id",rs.getString("id"));
obj.put("name",rs.getString("name"));
obj.put("mobile",rs.getString("mobile"));
obj.put("email",rs.getString("email"));
obj.put("dept",rs.getString("dept"));
list.add(obj);
//System.out.println("db: "+list.get(0));
}
rs.close();
stmt.close();
conn.close();
}catch(java.sql.SQLException e)
{
System.out.println("ERRRORHELLO");
e.printStackTrace();
}
return list;
}
}
Upvotes: 0
Views: 5935
Reputation: 1204
Add java code inside scriplet
<% %>
in jsp
function viewData()
{
var c="Hello";
document.getElementById("p1").innerHTML=c;
<% JSONArray j=request.getAttribute("jsonArray"); %>
document.getElementById("p1").innerHTML="<%=j%>";
<%String s=request.getAttribute("myname");%>
document.getElementById("p1").innerHTML="<%=s%>";
event.preventDefault();
}
Refer this link for scriplets
Upvotes: 1