How to pass Jsp values to Java?

This is jsp page (output.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>Insert title here</title>
</head>
<body>
<%@ page import = "guru.hashmap"%>
<% 

hashmap hm = new hashmap();

String inputvalue = request.getParameter("word"); 
String output = hm.HashmapValues(inputvalue);
out.println(inputvalue);
out.println("means");%> <br> <% 
out.println(output);%>

</body>
</html>

 

This is java program (hashmap.java):

package guru;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class hashmap {
    static String naveen;
    String nav;
    public String HashmapValues(String myguru){
    	HashMap<String, String> myMap = new HashMap<String, String>();
        myMap.put("ind", "india");
        myMap.put("tn", "tamilnadu");
        myMap.put("blr","bangalore");
        Set<Entry<String,String>> set = myMap.entrySet();
        for (Map.Entry<String, String> me : set) {
            if(me.getKey().equalsIgnoreCase(myguru)){
            	System.out.println("sucess");
                nav = me.getValue();
        }
            else{
            	System.out.println("failed");
            }
        }
		return nav;
    }        
}

 

This is jsp page (input.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>Insert title here</title>
</head>
<body>
<form method="post" action="output.jsp" name="form">
Enter the word: <input type="text" name="word">
<input type="submit">
</form>
</body>
</html>


In this program we are getting some value in the text box of input.jsp page.

Then we are passing this value to our java program(hashmap.java) as method arguments and retrieving the value in output.jsp.

Then displaying the output in output.jsp.

 

Thanks for reading this post…!!

Revert me if you have any doubts and clarifications………!!!

 


 

 

One comment

  • kavimani

    java file:
    class sam
    {

    int x;
    public void service(dynamohttpservlet req,dynamohttpservlet res)throws IOExpression,servlet exception
    x=Integer.parseInt(req.getparameter(“val_1”));

Leave a Reply