eclipse - Servlet and html form for calculator -
I want to create a calculator which basically connects two numbers, subtract, multiplied and divides. To get this all before, I have designed a form in HTML and have felt the desire to count the answers on the server, so I have written a code on servlet, but when I press the submit button of my form Nothing will happen. Note: I am working on eclipse, so you have been requested to answer my question regarding eclipse. Calculator. Java:
package mypackage; Import java.io * *; Import javax.servlet. *; Import javax.servlet.http.http. *; Public class calculator HTTerviewretlet {Public Zero Document (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {response.setContentType ("text / html"); PrintWriter out = response.getWriter (); Int a1 = integer. Parsient (request.getParameter ("n1")); Int a2 = integer.print (request.getParameter ("n2")); If (request.getParameter ("r1")! = Null) {out.println ("& lt; h1> add & lt; / h1 & gt;" + (a1 + a2)); } If (request.getParameter ("r2") = null) {out.println ("& lt; h1 & gt; substrate & lt; / h1 & gt;" + (a1-a2)); } If (request.getParameter ("r3") = null) {out.println ("& lt; h1> multiplication " + (a1 * a2)); } If (request.getParameter ("r1")! = Null) {out.println ("& lt; h1> division " + (a1 / a2)); }} Catch (Exception E) {}}}
index.html
& lt ;! DOCTYPE html public "- // W3C // DTD HTML 4.01 transitional // n" "http://www.w3.org/TR/html4/loose.dtd"> & Lt; Html & gt; & Lt; Top & gt; & Lt; Meta http-equiv = "content-type" content = "text / html; charset = ISO-885 9-1" & gt; & Lt; Title & gt; Calculator & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; H1 style = "text_align = center" & gt; Calculator & lt; / H1> & Lt; Form method = "get" action = "/ servlet" & gt; & Lt; Label & gt; First number: & lt; / Labels & gt; & Lt; Input type = "text" name = "N1" /> & Lt; Br / & gt; & Lt; Label & gt; Second number: & lt; / Labels & gt; & Lt; Input type = "text" name = "n2" /> & Lt; Br / & gt; & Lt; Div & gt; & Lt; Label & gt; & Lt; Input type = "radio" name = "r1" value = "add" /> Extra & lt; Br / & gt; & Lt; / Labels & gt; & Lt; Input type = "radio" name = "r2" value = "sub" /> Subtraction & lt; Br / & gt; & Lt; Input type = "radio" name = "r3" value = "parent" /> Multiplication & lt; Br / & gt; & Lt; Input type = "radio" name = "r4" value = "div" /> Partition & lt; Br / & gt; & Lt; / Div & gt; & Lt; Input type = "button" value = "submit" /> & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;
web.xml:
& lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: web = "http: //java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi: Schema Location =" http://java.sun.com/xml/ns/javaee http: //java.sun Com / Xml / ns / javaee / web-app_3_0.xsd "id =" WebApp_ID "version =" 3.0 "& gt; & Lt; Display-name & gt; Servlet & lt; / Display-name & gt; & Lt; Servlet & gt; & Lt; Servlet-name & gt; Servlet & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Mypackage.Calculator & lt; / Servlet category & gt; & Lt; / Servlet & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; Servlet & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / FirstHomePage & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; Welcome-file-list & gt; & Lt; Welcome-file & gt; Index.html & lt; / Welcome-file & gt; & Lt; / Welcome-file-list & gt; & Lt; / Web application & gt;
You wrote url-pattern 'firstHomePage' in web.xml for the subnet name calculator
& lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; Servlet & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / FirstHomePage & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt;
So that you should write in the form action i.e.
& lt; Form action = "firstHomePage" method = "get" & gt;
(you do not write in form action)
Whatever you type in the form action, it is checked in the URL-pattern of all servlet mapping of the match The related servlet name and related servlet class were searched.
Update:
& lt; Input type = "submit" & gt;
No button and your form will not be submitted. If you are using the button
then you have to type some javascript.
Comments
Post a Comment