Question

EDIT: WORKS NOW! THANKS! So anchor is not a submit.

how come I cannot get the value of 'opr' from an input using doGet/doPost? I'm getting some nulls.

Form:

<form method="post" role="form" name="frm">
    <div class="form-group">
        <input type="text" class="form-control" required="" placeholder="Title" id="newsboxTitle" name="title">
    </div>
    <textarea class="form-control" rows="3" id="newsbox" name="content"></textarea>

    <input type="hidden" name="page_action">
    <div>
        <a href="addNews" type="button" name="opr" class="btn btn-danger" value="1">Submit</a>
    </div>
</form>

Servlet(newsUpdate.java):

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    String opr = req.getParameter("title");
    JOptionPane.showMessageDialog(null, opr);
    getServletContext().getRequestDispatcher("/news.jsp").forward(req, res);
}

   public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    String opr = req.getParameter("title");
    JOptionPane.showMessageDialog(null, opr);
    getServletContext().getRequestDispatcher("/admin/news.jsp").forward(req, res);
}

also if I don't put doGet i get error 405.

here's my web.xml:

<servlet>
    <servlet-name>addNews</servlet-name>
    <servlet-class>processes.newsUpdates</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>addNews</servlet-name>
    <url-pattern>/admin/addNews</url-pattern>
</servlet-mapping>
Was it helpful?

Solution

Clicking an anchor and submitting a form are two different things. – Sotirios Delimanolis

Until that comment becomes an answer, here we go...

This also explains the 405 error. Clicking on a link performs a "get" request. Therefore doGet is called, not doPost. This form needs a "submit" button or a JavaScript callback which submits the form to the web server.

OTHER TIPS

Why does the code use JOptionPane.showMessageDialog(null, opr);? Get the value like any other parameter. The code shouldn't be mixing Java Swing in a servlet, the user will never see it.

req.getParameter("opr")

Also the form should use a button to submit:

<button type="submit" name="opr" class="btn btn-danger" value="1">Submit</button>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top