javascript onclick jsp value passing to servlet

Program: MyJsp.jsp

[html]
<%@ 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>Javadomain.in</title>
</head>
<script type="text/javascript">
function document123() {
var append_input = document.getElementById("txt").value;
document.location.href="/My_Workouts/MyServlet?nav=’"+append_input+"’";
// My_Workouts is the project name.
}
</script>
<body>
<label>Enter the text to pass Servlet </label>
<input type="text" id="txt" ></input>
<button type="button" onclick="document123()">Go to Servlet!</button></div>
</body>
</html>
[/html]

MyServlet.java

[java]
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(&quot;/MyServlet&quot;)
public class MyServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public MyServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String input_value = request.getParameter(&quot;nav&quot;);
System.out.println(&quot;In servlet &quot;+input_value);
}

}[/java]

 

 

Leave a Reply