1

I want to make the filtering using radio button event. And I've searched about calling java method into html, and found this link. And try to implement it.

HTML file

<form action="${pageContext.request.contextPath}/servlet" method="post">
      <input type="radio" value="1" name="radioFilter1"/> <br/>
      <input type="radio" value="2" name="radioFilter2"/> <br/>
      <input type="radio" value="3" name="radioFilter3"/> <br/>
</form>

SomeClass.java

public class SomeClass{
    public String value;

    public String filter1() {
        return value= "1";
    }

    public String filter2() {
        return value= "2";
    }

    public String filter3() {
        return value= "3";
    }
}

Controller.java

@RequestMapping("/servlet")
public String filterServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
    SomeClass s = new FilterSorting();
    if (request.getParameter("radioFilter1") != null) {
        s.value= s.filter1();
    } else if (request.getParameter("radioFilter2") != null) {
        s.value= s.filter2();
    } else if (request.getParameter("radioFilter3") != null) {
        s.value= s.filter3();
    }
    return "/newhtml.html";
}

@RequestMapping("/newhtml.html")
public String newhtml(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    FilterSorting s = new FilterSorting();
    list = Common.getList(dbSessionId, s.value);
    model.addAttribute("asnlist", asnList);
    return "/newhtml.html";
}

I'm using Spring framework version 4.0.0. And also I already tried using servlet, but it seems didn't work. What I want is, when I click radioFilter2 then it will call filter2() which is set the parameter value is 2, and so on. And then the value will use as parameter to load content. But from what I've tried, it didn't work. Please help me, thanks!

Community
  • 1
  • 1
Devan J.
  • 157
  • 1
  • 11
  • Did you implement radio button onclick event ? how do you want to call the servlet ? on radio button event click or after clicking the submit button ? – kswaughs Nov 10 '15 at 04:50
  • Sorry, I'm not yet implement the onclick event. Is it not enough to only call the `request.getParameter("radioFilter1")` ? – Devan J. Nov 10 '15 at 04:56
  • You cannot directly call a java method from browser. When user fills the data in html form, HTML page should provide a way to send this data to a servlet either by submit button or javascript onclick events. – kswaughs Nov 10 '15 at 05:01
  • @kswaughs If I have the javascript onclick events, what's this function do? Is it only to get the value like this `function clickHandler(){ document.getElementById('radioFilter1'); }` ? Thanks – Devan J. Nov 10 '15 at 06:39
  • Just out of curiousity: which resources are you using to learn Spring MVC? Lately I'm seeing in an increasing rate Spring MVC controller related questions wherein users incorrectly call them Servlets. I'd love to contact its author(s) to rectify this severe terminology mistake. To learn for yourself what Servlets really are, head to http://stackoverflow.com/tags/servlets/info – BalusC Nov 10 '15 at 07:19
  • @DevanJ. Reading the form data at client side is different than reading at server side. The link shared by BalusC has very good information on web pages and their terminologies. – kswaughs Nov 10 '15 at 07:22
  • @BalusC thanks for your correction, I learn from many resources on internet. I'm sorry if this is not related with the servlets. I think it's same, thanks anyway. So, I use spring mvc controller for this case, can you help me how to solve it using spring-mvc? – Devan J. Nov 10 '15 at 08:18
  • It's definitely not the same. They are just competitors. As to help, sorry I don't do Spring. I only do Java EE. – BalusC Nov 10 '15 at 08:21
  • Alright, thanks @BalusC – Devan J. Nov 10 '15 at 08:31

0 Answers0