我在html中有一个文本字段。现在我想从html页面获取数据并将其导入我的JSP页面。如何做到这一点?

时间:2022-01-10 06:56:25

I have created the input form in html.Now to accept the data in my jsp page should I assign a string variable to accept the data in jsp Or what variable should I use.I want to store the data in mysql,There also I have created a text column.

我已经在html中创建了输入表单。现在我接受jsp页面中的数据我应该分配一个字符串变量来接受jsp中的数据或者我应该使用什么变量。我想将数据存储在mysql中,我也有创建了一个文本列。

1 个解决方案

#1


1  

this is a simple example

这是一个简单的例子

1/ you create a jsp page where you put your form with inputs

1 /你创建了一个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 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> 

2/ create a servlet :

2 /创建一个servlet:

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);

  }  
}

3/ create another JSP page (test.jsp)

3 /创建另一个JSP页面(test.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 first JSP   </title>
    </head> 
    <body>      

          the color is : ${mycolor}         

    </body> 
</html> 

4/ in your web.xml file you should have (or put)

4 /在你的web.xml文件中你应该(或放)

<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>

#1


1  

this is a simple example

这是一个简单的例子

1/ you create a jsp page where you put your form with inputs

1 /你创建了一个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 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> 

2/ create a servlet :

2 /创建一个servlet:

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);

  }  
}

3/ create another JSP page (test.jsp)

3 /创建另一个JSP页面(test.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 first JSP   </title>
    </head> 
    <body>      

          the color is : ${mycolor}         

    </body> 
</html> 

4/ in your web.xml file you should have (or put)

4 /在你的web.xml文件中你应该(或放)

<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>