Data packaging and XSLT parsing data in servlet applications

xiaoxiao2021-03-06  54

Data packaging and XSLT parsing data in servlet applications

I wrote a small web application today: a simple landing interface (HTML), enter the user's name and mailbox. You can then click Submit. The submitted interface is a welcome interface - display the name of the user and the mailbox. This implementation process does not involve access to the database.

Let me talk about my realization.

1. Get the data entered from the landing interface, construct a subclass of Javax.serveT.Filter to process (package) to get data, use the Document instance (DOM) in org.w3c.dom package to package this data, and put These data is placed in the instance of HTTPSession

2. In the LoginServlet (subcategory of javax.servlet.http.servlet), use the previously write XSLT file to parse the Document object (DOM) in the HTTPSession instance, and send the resolution result to response.getwriter (), users Seeing that the original window is converted into a successful window to display the information you just entered.

This is an example of a combined with the servlet, as well as XML and XSLT.

As follows:

INDEX.HTML

login - filtertest </ title></p> <p></ hEAD></p> <p><body></p> <p><H1> Input Your ID and email </ h1></p> <p><hr /></p> <p><form action = "/ login" method = "post"></p> <p><table></p> <p><tr></p> <p><TD> Name: </ TD></p> <p><TD></p> <p><Input name = "name" type = "text" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD> Email: </ TD></p> <p><TD></p> <p><Input name = "email" type = "text" /></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD /></p> <p><TD></p> <p><Input name = "submit" type = "submit" value = "save" /></p> <p></ td></p> <p></ TR></p> <p></ TABLE></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>InfoFilter.java</p> <p>Import javax.servlet. *;</p> <p>Import javax.servlet.http. *;</p> <p>Import java.io. *;</p> <p>Import java.util. *;</p> <p>Import org.w3c.dom. *;</p> <p>Import javax.xml.parsers. *;</p> <p>Public Class Infofilter IMPLEments FILTER</p> <p>{</p> <p>PRIVATE FILTERCONFIG FCONFIG = NULL;</p> <p>PUBLIC Infofilter () {</p> <p>}</p> <p>Public void init (FilterConfig FC) throws servletexception {</p> <p>Fconfig = fc;</p> <p>}</p> <p>Public void destroy () {</p> <p>Fconfig = NULL;</p> <p>}</p> <p>Public Void Dofilter (Final ServletRequest Request, Final ServletResponse Response, Filterchain Chain)</p> <p>THROWS IOException, servletexception {</p> <p>HTTPSERVLETREQUEST HTTPREQUEST = (httpservletRequest) Request;</p> <p>Enumeration paramnames = httpRequest.getParameterNames ();</p> <p>Try {</p> <p>DocumentBuilderFactory DBF = DocumentBuilderFactory.newinstance ();</p> <p>DocumentBuilder docbuilder = dbf.newdocumentbuilder ();</p> <p>Document doc = docbuilder.newdocument ();</p> <p>ELEMENT INFO = Doc.createElement ("info");</p> <p>Doc.Appendchild (Info);</p> <p>While (paramnames.hasmoreElements ()) {</p> <p>String paramname = (string) paramnames.nextelement ();</p> <p>String paramvalue = (string) (paramname); paramname [0]);</p> <p>Element elem = doc.createElement (paramname);</p> <p>ELEM.APPENDCHILD (Doc.createTextNode (paramvalue);</p> <p>INFo.Appendchild (ELEM);</p> <p>}</p> <p>HttpSession session = httpRequest.getations;</p> <p>Session.SetaTribute ("DOC", DOC);</p> <p>Chain.dofilter (httpRequest, response);</p> <p>} catch (ParserConfigurationException E) {</p> <p>PrintWriter PW = response.getwriter ();</p> <p>PW.Println ("<html> <head> <title> error </ title> </ head>");</p> <p>PW.Println ("<body> <h1> Error Happened in Building Domtree <H1> </ Body> </ HTML>");</p> <p>}</p> <p>}</p> <p>}</p> <p>LoginServlet.java</p> <p>Import java.io. *;</p> <p>Import java.net. *;</p> <p>Import javax.servlet. *;</p> <p>Import javax.servlet.http. *;</p> <p>Import javax.xml.transform. *;</p> <p>Import javax.xml.transform.dom. *;</p> <p>Import javax.xml.transform.stream. *;</p> <p>Import org.w3c.dom. *;</p> <p>Import javax.xml.parsers. *;</p> <p>Public Class loginservlet Extends httpservlet</p> <p>{</p> <p>Protected void dopost (httpservletRequest request, httpservletResponse response) {</p> <p>HttpSession session = request.getations (TRUE);</p> <p>Document doc = (document) session.getattribute ("doc"); try {</p> <p>TransformerFactory Transfact = TransformerFactory.newInstance ();</p> <p>String xltname = "/web-inf/xslt/info.xslt";</p> <p>URL URL = GetServletContext (). Getresource (xsltname);</p> <p>String systemid = url.toExternalForm ();</p> <p>Templates Infotemplate = Transfact.Newtemplates (New StreamSource (SystemID));</p> <p>Transformer Trans = Infotemplate.NewTransformer ();</p> <p>Response.setContentType (Text / HTML ");</p> <p>PrintWriter PW = response.getwriter ();</p> <p>Trans.Transform (New Domsource (DOC), New StreamResult (PW));</p> <p>session.INVALIDATE ();</p> <p>} catch (exception e) {</p> <p>E.PrintStackTrace ();</p> <p>session.INVALIDATE ();</p> <p>}</p> <p>}</p> <p>}</p> <p>Info.xslt</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/xsl/transform"</p> <p>XMLns = "http://www.w3.org/1999/xhtml"></p> <p><xsl: Output method = "xml" indent = "yes" encoding = "UTF-8"</p> <p>DOCTYPE-PUBLIC = "- // w3c // DTD XHTML 1.0 Transitional // EN"</p> <p>DOCTYPE-System = "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" /></p> <p><XSL: Template Match = "/"></p> <p><html></p> <p><HEAD></p> <p><Title></p> <p><XSL: Text> Welcome, </ XSL: Text></p> <p><XSL: Value-of SELECT = "/ info / name" /></p> <p></ title></p> <p></ hEAD></p> <p><body></p> <p><tr></p> <p><h1></p> <p><TD> <XSL: Text> Welcome, </ XSL: Text> </ TD></p> <p><TD> <XSL: Value-of Select = "/ info / name" /> </ td></p> <p></ h1></p> <p></ TR></p> <p><hr /></p> <p><tr></p> <p><h3></p> <p><TD> <XSL: Text> Your Email IS: </ XSL: Text> </ TD></p> <p><TD> <XSL: Value-of Select = "/ info / email" /> </ td> </ h3></p> <p></ TR></p> <p><hr /></p> <p><tr></p> <p><p align = "right" /></p> <p><a href="/index.html"> back to homepage </A></p> <p></ TR></p> <p></ body></p> <p></ html></p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>Web.xml</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><web-app xmlns = "http://java.sun.com/xml/ns/j2ee"</p> <p>XMLns: xsi = "http://www.w3.org/2001/xmlschema-instance"</p> <p>XSI: schemAlocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"</p> <p>Version = "2.4"></p> <p><servlet></p> <p><servlet-name> Login </ servlet-name></p> <p><servlet-class> loginservlet </ servlet-class></p> <p></ servlet></p> <p><servlet-mapping></p> <p><servlet-name> Login </ servlet-name></p> <p><url-pattern> / login </ url-pattern></p> <p></ servlet-maping></p> <p><filter></p> <p><filter-name> infofilter </ filter-name></p> <p><filter-class> infofilter </ filter-class></p> <p></ filter></p> <p><filter-mapping></p> <p><filter-name> infofilter </ filter-name></p> <p><url-pattern> / login </ url-pattern></p> <p></ filter-mapping></p> <p></ web-app></p> <p>deploy:</p> <p>INDEX.HTML</p> <p>SRC</p> <p>InfoFilter.java</p> <p>LoginServlet.java</p> <p>WEB-INF</p> <p>Web.xml</p> <p>Classes</p> <p>InfoFilter.class</p> <p>LoginServlet.class</p> <p>xslt</p> <p>Info.xslt</p> <p>Postscript: At the time of debugging, a problem is found, the work in a web application is very large. When writing code, writing "InfoFilter>" in <filter-name> in <filter-maping>, "InfoFilter>" results in the browser are invisible to call index.html. It can be seen that Tomcat is strict in implementing Web.xml requirements in a web application. If you have a 404 error in debug this program application, you may wish to check the web.xml file carefully.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-116653.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="116653" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.042</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'DThjdo8MfrI8_2BD9Ny5NV8r6C_2F8Do2oRS0TsBDXkV7USeJaCwbvVA8cFJwDw93kVG8ACSAxAlbIZXgsuN'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>