0

I have below a jps with below code with source and destination drop down and a button "Execute" which will call a servlet.

The servet will perform some operation based on the values selected.

JSP Code:

<%@ 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/>
    </head>
    <form action="MySourceEnv" method="POST">
        <select name="SourceEnv" >      
            <option>10.100.10.11</option>      
            <option>10.100.10.12</option>      
        </select>   
    </form>
    <form action="MyDestEnv" method="POST">
        <select name="DestEnv" >      
            <option>10.100.10.11</option>      
            <option>10.100.10.12</option>      
        </select>   
    </form>
    <body>
        <button onclick="location.href = 'http://localhost:7500/Project_1/JavaServlet';" id="RedirectButton" > Execute</button>
    </body>
</html> 

Servlet Code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class JavaServletClass extends HttpServlet {

    public void init() throws ServletException {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String SourceEnvParam = request.getParameter("SourceEnv");
        out.println("<h1>" + SourceEnvParam + "</h1>");
        LogicMethod(SourceEnvParam);
    }

    private void LogicMethod(String SourceEnvParam) throws IOException {
        // Some logic here
    }

    public void destroy() {
    }
}

I'm getting the value of request.getParameter("SourceEnv") as Null when execute button is clicked and servlet is called.

What's wrong I'm doing here?

0

1 Answer 1

3

I think problem created in your html code. First of all you created two form out of your body which has two action. But you defined another another action with

<button onclick="location.href = 'http://localhost:7500/Project_1/JavaServlet';" id="RedirectButton" > Execute</button>. 

Try writing your jsp page like this

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

        <title> name<title/>
    </head>

    <body>
      <form action="JavaServletClass" method="GET">
        <select name="SourceEnv" >      
            <option>10.100.10.11</option>      
            <option>10.100.10.12</option>      
        </select>  
       <select name="DestEnv" >      
            <option>10.100.10.11</option>      
            <option>10.100.10.12</option>      
        </select> 
<button type="submit" value="Submit">Submit</button> 
</form>
</body>
</html> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.