java - I have a text field in html.Now i want to take the data from the html page and import it to my JSP page.How to do that? -


i have created input form in html.now accept data in jsp page should assign string variable accept data in jsp or variable should use.i want store data in mysql,there have created text column.

this simple example

1/ create jsp page put form inputs

<%@ 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> first jsp   </title>     </head>      <body>               <form action="helloservlet" method="get">                         please enter color <br>             <input type="text" name="color" >             <input type="submit" value="submit">                                 </form>          </body>  </html>  

2/ create 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 method store data in database exp insertindb(color);      request.setattribute("mycolor", color);// if want see data      request.getrequestdispatcher("test.jsp").forward(request, response);    }   } 

3/ create jsp page (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> first jsp   </title>     </head>      <body>                  color : ${mycolor}               </body>  </html>  

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

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

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -