简体   繁体   中英

Getting null from radiobutton value in servlet

(I checked all the other similar questions and I am a newbie let me say first)

I have html in very basic terms like this: https://ibb.co/KVNbv9z And a servlet code like below.

which gives null return for the String q1 :/

I think the main problem is I am trying to use request.getRequestDispatcher("htmlquestions.html").include(request, response); inside the servlet , but can not get return its result back. What should I do- I am searching for 2 days for this. Appreciated big for your help

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.logging.Logger;


public class SecondServlet extends HttpServlet {




public void doGet(HttpServletRequest request, HttpServletResponse     response){
try{


response.setContentType("text/html");
PrintWriter out = response.getWriter();


request.getRequestDispatcher("htmlquestions.html").include(request,     response); 


String q1 = request.getParameter("Q1");

out.print(  " aaaaaaaaaaaaaaaaaaa     " + q1 );


out.print("<form action='ThirdServlet'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");


out.close();

}catch(Exception e){System.out.println(e);}
}

}

From the Java EE docs:

https://javaee.github.io/javaee-spec/javadocs/javax/servlet/RequestDispatcher.html

void include(ServletRequest request, ServletResponse response) throws ServletException, IOException

Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

You are just including content in the response , the request is not changed. The request parameters are set when the form is submitted. This is why request.getParameter returns null, there is no value set when the request is received by the servlet.

To do what you want, you should create a JSP form, or better a JSF form that includes the content on page rendering. I strongly suggest you to use JSF, as using servlets and/or JSP to create Web content is somewhat deprecated and can get very messy as the content becomes more complex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM