create array of html image objects for java servlet -
my goal create array store several images, display each time hit next/prev button on webpage.
so far have don't know if initializing correctly images not appear in browser
import javax.servlet.http.*; import javax.servlet.*; import java.io.*; import java.sql.*; import java.io.*; import java.util.*; public class hitservlet extends httpservlet { private int mcount; public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // set response content type response.setcontenttype("text/html"); printwriter out = response.getwriter(); out.println("<html>\n" + "<body>\n" + "<form action=\"/midp/hits\" method=\"post\">\n" + "first name: <input type=\"text\" name=\"first_name\">\n" + "<br />\n" + "last name: <input type=\"text\" name=\"last_name\" />\n" + "<input type=\"submit\" value=\"submit\" />\n" + "</form>\n</body>\n</html\n"); } // method handle post method request. public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); response.setcontenttype("text/html"); string title = "using post method read form data"; string [] images = new string[3]; images[0] = "<img src=\"images/cute.jpg\" alt=\"cat\">"; images[1] = "<img src=\"images/dog.jpg\" alt=\"dog\">"; images[2] = "<img src=\"images/koala.jpg\" alt=\"koala\">"; random rand = new random(); int index = rand.nextint(images.length); string doctype = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(doctype + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n" + "<ul>\n" + " <li><b>first name</b>: " + request.getparameter("first_name") + "\n" + " <li><b>last name</b>: " + request.getparameter("last_name") + "\n" + "</ul>\n" + images[index] + "\n" + "<form action=\"/midp/hits\" method=\"post\">\n" + "<input type=\"submit\" value=\"previous\" id=\"prevbtn\" />" + "\n" + "<button id=\"nextbtn\" >next</button>" + "\n" + "</form></body></html>"); } }
web.xml
<?xml version="1.0" encoding="iso-8859-1"?> <!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>bob</servlet-name> <servlet-class>hitservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>bob</servlet-name> <url-pattern>/hits</url-pattern> </servlet-mapping> </web-app>
the end goal call rand() function on array every time hit next or previous button. or suggestions appreciated!
Comments
Post a Comment