简体   繁体   中英

How to read data from HTML date input on Servlet?

<label>Birthday: </label> <input type="date" name="dob">

I have tried reading the value using dob , but it gave an error.

What about google?

<form name="loginForm" method="post" action="loginServlet">
    Username: <input type="text" name="username"/> <br/>
    Password: <input type="password" name="password"/> <br/>
    <input type="submit" value="Login" />
</form>

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
 
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
 
        String username = request.getParameter("username");
 
    }
 
}

Make Sure,if you have defined 'POST' in form,then the corresponding servlet code should be doPost()

Try This:

HTML:

    <form name="data" action="Test" method="POST">
    <label>Birthday: </label> <input type="date" name="dob">
    <input type="submit" value="submit">        
    </form>

Servlet:(inside doPost)

        String date=request.getParameter("dob");
        System.out.println("dateString:"+date);
        Date utilDate=new SimpleDateFormat("yyyy-mm-dd").parse(date); 
        System.out.println("date:"+utilDate);

Note: Please give more details about a question such as how you want to read data from HTML to servlet eg: Using form tag, using AJAX, using javascript redirect. As people on stackoverflow take efforts to provide solution.Kindly take Some effort while posting question.

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