html - Java Servlets - Make hello world appear on button press -


for school learning servlets, , although understand idea behind them, unsure of how use them in practice. part of piece of work, must create web page in netbeans contains button, , when pressed must invoke servlet display message 'hello, world!'.

i have web-page, 'gethellomessage.xhtml'

<body>       <div>click button show message.</div>       <p></p>       <button type="button">get message</button> </body> 

and 'helloworldservlet.java'

@webservlet(urlpatterns = {"/helloworldservlet"}) public class helloworldservlet extends httpservlet {     @override     protected void doget(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception     {         response.setcontenttype("text/html");          printwriter out = response.getwriter();         out.println("<h1>hello, world!</h1>");     } } 

how can invoke servlet when user presses button?

to working had edit glassfish-web.xml (located in project> web-inf) , add following lines of code:

<servlet>          <servlet-name>helloworldservlet</servlet-name>          <servlet-class>helloworldservlet</servlet-class> </servlet> 

and in gethellomessage.xhtml :

<form action="helloworldservlet" method="get">     <button type="submit">get message</button> </form> 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -