作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我能够将 NewFile.jsp 中的搜索词传递给 HelloWorld servlet 并在 wecome jsp 中显示结果。我在新的java类中编写了另一个程序。我不知道如何将searchterm传递给java类。从而运行程序。
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);
}
}
其他 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);}
}
}
如何将 Myprogram.java 中的变量 searchterm 与 HelloWorld.java 中的变量链接(将“google”替换为实时用户输入的搜索词),因为 HelloWorld.java 是一个可以显示到 jsp 的 servlet。
最佳答案
使用以下代码:
ParameterPy类
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);
}
}
对于注释中的查询,现在您正在 System.out
上打印 ParameterPy 的结果。为了在 jsp
网页上显示结果,您需要 JspWriter
对象,即 out
。
为此,我修改了您的逻辑,如下所示,将结果返回为 String
。这个String
结果将显示在jsp页面上。
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>
关于java - 如何在servlet中使用或调用java类并将结果显示到jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39970352/
我是一名优秀的程序员,十分优秀!