我是java编程的新手,我正在尝试使用struts将一个值从servlet传递给jsp中的javascript。
servlet类 -
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>
当我取消注释getAttribute(&#34; myname&#34;)时,段落标记中没有显示任何内容。我想知道是否有任何方法可以在不使用JQuery或Ajax的情况下将值从servlet检索到javascript暂时。
//使用显示的完整代码进行编辑 -
的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和handler.java已经显示
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;
}
}
答案 0 :(得分:1)
在jsp
中的scriplet
<% %>
内添加java代码
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();
}
请参阅此link以获取scriplets