java - Writing a simple servlet. Getting and sending data via servlet -
I'm a newbie in servlet writing. I need to write a simple servlet, it should have input and output data via form. For example, in my service I have input data about some car (this includes such features: car name, car size, and car color). The servicelet has to save these data and also, it will show saved data. I have done something, but I still do not know about completing.
This is an html-code page, the subnet calls it:
& Lt; Title & gt; Cars & lt; / Title & gt; & Lt; / Head & gt; K & lt; Body & gt; & Lt; Center & gt; Cars & lt; / Center & gt; & Lt; P & gt; Name: & lt; / P & gt; & Lt; Form id = "form1" name = "form1" method = "post" verb = "" & gt; & Lt; Label = "car_name"> gt; & Lt; / Label & gt; & Lt; Input type = "text" name = "car_name" id = "car_name" /> & Lt; / Form & gt; & Lt; P & gt; Color: & lt; / P & gt; & Lt; Form id = "form2" name = "form2" method = "post" action = "" & gt; & Lt; Label = "car_name"> gt; & Lt; / Label & gt; & Lt; Input type = "text" name = "car_color" id = "car_color" /> & Lt; / Form & gt; & Lt; P & gt; Size: & lt; / P & gt; & Lt; Form id = "form3" name = "form3" method = "post" verb = "" & gt; & Lt; For label = "car_size" & gt; & Lt; / Label & gt; & Lt; Input type = "text" name = "car_size" id = "car_size" /> & Lt; / Form & gt; & Lt; Input name = "send" type = "button" value = "send" /> & Lt; Input name = "get_out" type = "button" value = "output" /> & Lt; Textarea name = "output" cols = "10" rows = "10" read-only = "read only" & gt; & Lt; / Textarea & gt; & Lt; Fieldset & gt; & Lt; Legend & gt; Test simple servlets & lt; / Legend & gt; & Lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "carServlet" & gt; Carveratelet & lt; / A & gt; Carveratlet is a servlet that carries data characteristics of cars and lieutenant; / Li & gt; & Lt; / Ul & gt; & Lt; / Fieldset & gt; & Lt; / Body & gt; & Lt; / Html & gt; and servlet code (Java), (I only realized it):
package test package; Import javax.servlet.annotation.WebServlet; Import javax.servlet.http.httpServlet; @WebServlet ( "/ carServlet") public class CarServlet HttpServlet {// some code provides web-finds items on the page // only example string: String item = (String) getItem ( "car_name"); } How can it be completed to save the servlet? Save saved data and touch it?
package test package; Import javax.servlet.annotation.WebServlet; Import javax.servlet.http.httpServlet; @WebServlet ( "/ carServlet") public class CarServlet HttpServlet {protected void increases doGet (HttpServletRequest request, HttpServletResponse response) ServletException, IOException {doPost (throws request, response); } Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub string Karnam = request.getParameter ( "car_name"); System.out.println ("car name:" + event name); }} Try to apply it to your doGet and doPost method within your Srwolet. You are submitting a form through a post so that you can keep your code there. I just forwarded the post method to get it. You can access the parameter by request.getParameter ("parameterName");
Comments
Post a Comment