I have a String string1 = "123"
on a page (www.paginaexample.com) and I have a input name="input1" type="text"
, how to insert the content of string1
into input1
, and the entire process is done in JAVA (eclipse) ??
I have a String string1 = "123"
on a page (www.paginaexample.com) and I have a input name="input1" type="text"
, how to insert the content of string1
into input1
, and the entire process is done in JAVA (eclipse) ??
First of all, there is a great example answer about servlet and jsp in stackoverflow brazil : Using JSP and JAVA
Responding objectively to your question, the basic way, you need to implement a Servlet :
public class MeuServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String string1 = "123";
request.setAttribute("minhaString", string1);
request.getRequestDispatcher("/minhaPagina.jsp").forward(request, response);
}
}
myPage.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>Minha Pagina JSP</title>
</head>
<body>
Meu input:
<input type="text" name="meuInput" value='<%=request.getAttribute("minhaString")%>'/>
</body>
</html>
index.html
<!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">
</head>
<body>
<h1 align=center>Load JSP</h1>
<form method=GET action=MeuServlet.do>
<input value="chama meu servlet" type=submit>
</form>
</body>
</html>
web.xml (web deployment descriptor)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Meu Servlet</servlet-name>
<servlet-class>com.example.web.MeuServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Meu Servlet</servlet-name>
<url-pattern>/MeuServlet.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>