Reputation: 195
I am able to pass the searchterm in NewFile.jsp to HelloWorld servlet and display the result in wecome jsp. I have another program wrote in the new java class.I do not know how to pass the searchterm to the java class. so as to run the program.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
NewFile.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> My JSP page </title>
</head>
<body>
<form method="post" action="HelloWorld">
Please enter a Keyword <br>
<input type="text" name="searchTerm"size="20px">
<input type="submit" value="submit">
</form>
</body>
</html>
welcomepage.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>
<%String X = (String)request.getAttribute("searchTerm");%>
The search word is<%=X %>
<%-- welcom <%=request.getAttribute("searchTerm")%> --%>
</body>
</html>
servlet: HelloWorld.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class HelloWorld extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// ..........reading the user input
String searchTerm= request.getParameter("searchTerm");
request.setAttribute("searchTerm",searchTerm);
request.getRequestDispatcher("welcomepage.jsp").forward(request, response);
}
}
the other javascript: Myprogram.java
public class ParameterPy {
public static void main(String a[]){
try{
String searchTerm="google";
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://program.py",""+searchTerm);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
}
System.out.println("........end process.......");
}catch(Exception e){System.out.println(e);}
}
}
How do I link the variable searchterm in Myprogram.java with the variable in HelloWorld.java (replace the "google" to the real time user input search term) since the HelloWorld.java is a servlet can display to the jsp.
Upvotes: 0
Views: 11440
Reputation: 3914
Use the below code:
ParameterPy class
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ParameterPy
{
public static void myMethodName(String searchTerm)
{
try
{
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python", "D://program.py", ""+ searchTerm);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null)
{
System.out.println("Python Output: " + line);
}
System.out.println("........end process.......");
} catch (Exception e)
{
System.out.println(e);
}
}
}
HelloWorld Servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// ..........reading the user input
String searchTerm= request.getParameter("searchTerm");
ParameterPy.myMethodName(searchTerm);//Call to java class method
request.setAttribute("searchTerm",searchTerm);
request.getRequestDispatcher("welcomepage.jsp").forward(request, response);
}
}
For your query in comments, right now you are printing the result of ParameterPy on System.out
. In order to display the result on jsp
webpage you need JspWriter
object, i.e, out
.
For this I have amended your logic as below to return result as String
. This String
result will be displayed on jsp page.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ParameterPy
{
public static String myMethodName2(String searchTerm)
{
StringBuilder sb=null;
try
{
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python", "D://program.py", ""+ searchTerm);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
sb=new StringBuilder();
sb.append(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null)
{
sb.append("Python Output: " + line);
}
sb.append("........end process.......");
} catch (Exception e)
{
System.out.println(e);
}
return sb.toString();
}
}
JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="stack.filter.ParameterPy"%>
<!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>
<%
String searchTerm = request.getParameter("searchTerm");
out.println(ParameterPy.myMethodName2(searchTerm));
%>
</body>
</html>
Upvotes: 1