Java 我在html中有一个文本字段。现在我想从html页面获取数据并将其导入我的JSP页面。如何做到这一点?
我已经用html创建了输入表单。现在要在我的jsp页面中接受数据,我应该分配一个字符串变量来接受jsp中的数据,还是应该使用哪个变量。我想在mysql中存储数据,我还创建了一个文本列 这是一个简单的例子 1/您创建了一个jsp页面,将表单与输入一起放入其中Java 我在html中有一个文本字段。现在我想从html页面获取数据并将其导入我的JSP页面。如何做到这一点?,java,html,mysql,jsp,Java,Html,Mysql,Jsp,我已经用html创建了输入表单。现在要在我的jsp页面中接受数据,我应该分配一个字符串变量来接受jsp中的数据,还是应该使用哪个变量。我想在mysql中存储数据,我还创建了一个文本列 这是一个简单的例子 1/您创建了一个jsp页面,将表单与输入一起放入其中 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE
<%@ 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 first JSP </title>
</head>
<body>
<form action="HelloServlet" method="GET">
Please enter a color <br>
<input type="text" name="color" >
<input type="submit" value="submit">
</form>
</body>
</html>
3/创建另一个JSP页面(test.JSP)
我的第一个JSP
颜色为:${mycolor}
4/在web.xml文件中,您应该拥有(或放置)
你好
你好世界
你好
/HelloServlet
我不明白,如果你想获取数据,为什么要使用变量,只需在表单中调用servlet即可-action=“SERVELT-NAME”请你详细说明我不明白你的意思。因为我是初学者谢谢分享,我会努力理解并尝试。
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
{
String color= request.getParameter("color");
// HERE CALL A METHOD TO STORE DATA IN DATABASE exp insertInDB(color);
request.setAttribute("mycolor", color);// if you want to see your data
request.getRequestDispatcher("test.jsp").forward(request, response);
}
}
<%@ 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 first JSP </title>
</head>
<body>
the color is : ${mycolor}
</body>
</html>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>