java ee - how to translate servlet to JSP? -


can me translate servlet jsp

here's code:

package inventory;  import java.io.*; import javax.servlet.*; import javax.servlet.http.*;  public class displaydata extends httpservlet {  public void doget(httpservletrequest request,         httpservletresponse response)         throws servletexception, ioexception {      response.setcontenttype("text/html");     printwriter out = response.getwriter();     item item = (item) request.getattribute("invenitem");      if (item != null) {         out.println("<html><title>inventory item</title>");         out.println("<body><h1>inventory item details:</h1>");         out.println("stock id  : " + item.getstockid() + "<br/>");         out.println("name      : " + item.getitemname() + "<br/>");         out.println("unit price: " + item.getunitprice() + "<br/>");         out.println("on stock  : " + item.getonstock() + "<br/>");         out.println("</body>");         out.println("</html>");     } else {         requestdispatcher rd =            request.getrequestdispatcher("/searchpage.html");         rd.include(request, response);         rd = request.getrequestdispatcher("/adddata.html");         rd.include(request, response);     }  } 

}

i trying use scriplets, still want know how convert:

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; 

to jsp. try this:

<html> <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8">     <title>display data</title> </head> <jsp:usebean id="inventory" class="inventory.adddata" /> <jsp:directive.page import="java.io.*" /> <jsp:directive.page import="javax.servlet.*" /> <jsp:directive.page import="javax.servlet.http.*" /> <body>      <%-- don't know how convert this:          public class displaydata extends httpservlet          in jsp     --%>  </body> 

please help... in advance

the cleanest way use jstl instead of scriptlets (check out good primer here). in nutshell, need have jstl jars installed (either in app server or in specific webapp). then, can following:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> ... <c:out value="${invenitem.stockid}"/> 

the first line imports jstl "core" tag library, gives access basic tags. then, tag used output data - escape special characters (for example, suppose stockid has character "<"). finally, ${} el expression, in case accesses invenitem request attribute , extracts stockid value (by calling getstockid()).

clean, simple, no ugly java scriptlets in jsp view.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -