将变量从 JSP 传递给 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16022117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a variable from JSP to Java
提问by kuks
I am trying to pass a string variable called seq
from a JSPto a Java program, and pass it on to the another Java program by passing the string as an argument to its object. I am somehow getting stuck.
我试图将一个seq
从JSP调用的字符串变量传递给一个 Java 程序,并通过将该字符串作为参数传递给另一个 Java 程序来将它传递给另一个 Java 程序。我不知何故陷入困境。
Start.jsp:
开始.jsp:
<%@ page import="org.dypbbi.nirmiti.ProtModMain %>
<%@ 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>NIRMITI</title>
</head>
<body>
<h1>Please wait...</h1>
<%
String seq=request.getParameter("s");
ProtModMain.getSequence(seq);
%>
</body>
</html>
ProtModMain.java:
ProtModMain.java:
package org.dypbbi.nirmiti;
public class ProtModMain {
String sequence="";
public static String getSequence(String str)
{
return str;
}
public static void main(String args[])throws Exception
{
ProtModMain MainObj = new ProtModMain();
sequence = MainObj.getSequence();
new ObjectFactory(sequence);
}
}
Start.jsp will retrieve the string value from the HTML. It passes the string to ProtModMain
class via the method getSequence
. I now need to use the string value to pass it to other classes that require it, so I intend to pass it a parameter to the ObjectFactory
object. But before that, I need to call the getSequence
method in the ProtModMain
class so that I can pass the value. I am not understanding how to call the getSequence
method in the main method of ProtModMain
class.
Start.jsp 将从HTML检索字符串值。它通过方法将字符串传递给ProtModMain
类getSequence
。我现在需要使用字符串值将它传递给需要它的其他类,因此我打算将它作为一个参数传递给ObjectFactory
对象。但在此之前,我需要调用类中的getSequence
方法,ProtModMain
以便我可以传递值。我不明白如何getSequence
在ProtModMain
类的主要方法中调用该方法。
回答by Sujay
You need to set the parameter to the request using request.setParameter("<name>",<value>);
. Then you can get it in the Java file using HTTPRequest getParameter("<name>");
.
您需要使用 将参数设置为请求request.setParameter("<name>",<value>);
。然后您可以使用HTTPRequest getParameter("<name>");
.
回答by elopez
You can use <form>
tags and <input type='hidden'>
with an <input type='submit'>
button, in the form you will specify the method to send and to where send the data.
您可以使用<form>
标签和<input type='hidden'>
带有<input type='submit'>
按钮,在表单中,您将指定要发送的方法和其中发送数据。
Or you can store in POJOs and store in session and recover it with a servlet.
或者您可以存储在 POJO 中并存储在会话中并使用 servlet 恢复它。
Or use Ajax with an XmlHttpRequest.
或者将 Ajax 与 XmlHttpRequest 一起使用。
回答by Fernando M. Pinheiro
You are not calling the main
method. In your JSP you are calling only the static getSequence
that, by the way, only returns the value.
您没有调用该main
方法。在您的 JSP 中,您只调用静态getSequence
,顺便说一下,只返回值。
I think you have a project concept problem: why your web (JSP) app has a main class?
我认为您有一个项目概念问题:为什么您的 Web (JSP) 应用程序有一个主类?
I think you should adapt:
我认为你应该适应:
Start.jsp:
开始.jsp:
<%@ page import="org.dypbbi.nirmiti.ProtModMain %>
<%@ 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>NIRMITI</title>
</head>
<body>
<h1>Please wait...</h1>
<%
String seq=request.getParameter("s");
ProtModMain protModMain = new ProtModMain();
ObjectFactory myFactory = protModMain.createFactory(seq);
//do whatever you want with your factory ;)
%>
</body>
</html>
ProtModMain.java:
ProtModMain.java:
package org.dypbbi.nirmiti;
public class ProtModMain {
public ObjectFactory createFactory(final String sequence) {
return new ObjectFactory(sequence);
}
}
This way you will be calling the methods you intended to.
这样你就可以调用你想要的方法。