Introduction to Servlet

xiaoxiao2021-03-06  36

Introduction to Servlet

table of Contents:

About servlets

Example Structure Description Lifecycle how to write a servlet program

The client's interactive Lifecycle provides information about servletrunner to run a servlet program with servletrunner.

About servlets

Servlets is a new new feature in Java 2.0.

Java Servlets is a module running on the request /-oriented request server, such as a Java-EnableD web server, and a similar extension. For example, a servlet can get data from an HTML order table and then use some commercial algorithms. Update the company's appropriate order database.

That is to say: Servlet can expand the web server function like the CGI script, but servlet takes up few intensive resources, there are many sites prepared using CGI scripts, due to the increase in visits, performance is rapidly decline, this is a shortcoming of CGI scripts, related CGI script concept, please refer to our "CGI Getting Started". At the same time, because servlets are written in Java, it is a cross-platform. The actual servlet is the real start of e-commerce.

Servlet API, is used to write servlet, write servlets, such as if you have a servlet that is loaded, what is the server environment that is loaded, or the protocol used to transfer data, so servlets Can be fused in different web servers.

Servlet can be quite effectively replaced with CGI scripts: it can easily have easy to write and run fast text. It can be easily commissioned to find out programs. The servlet program is developed with Java Servlet API, A Standard Java Extension. But not Part of the Java core framework can be used as a common additional product package to be used by merchants.

Example

Here are some servlet applications:

Used to process the HTML form generates Posted data through HTTPS, including trading order or credit card data. Therefore, servlet can become part of the order processing system, and work with the product inventory database, may be available on the online payment system. Allow people to cooperate between people A servlet can process multiple requests; they can use synchronous request support systems such as online meetings. Transfer requests. Servlet can transfer requests to other servers and servlets. This allows several servers on the same content in mirror Between balanced load. According to the type or organization range, it can be allowed to be used to divide logically in several servers. Servlet writers can define the activation agents that work together with each other, each agent is a servlet And the agent can transfer data between them.

Servlet structure

Before you have the specific servlet, you must understand the Java language. The following is based on your understanding of Java, the most important thing in the Servlet API is the servlet interface. All servlets IMPLEMENT (execution) this interface, the way, or directly, or via the extending Class, such as httpservlet This servlet interface provides methods for arranging Servlet and client. Servlet writers can provide more or all of the following methods when they develop Servlet programs.

When a servlet receives a call request from the client, it receives two objects: one is servletRequest, and the other is servletResponse. This servleTRequest class is summarized from the client to the server, and the servletResponse Class is summarized from servlet to the client connection.

ServletRequest Interface can get such information, such as the elaboration name transmitted by the client, the client is using the protocol, generates a request and receives a remote host name of the requested server. It also provides servlet, servletinputstream, servletinputStream, these data is The HTTP POST and PUT methods are submitted in client references. A substext of a servletRequest allows the servlet to get more protocol characteristics. For example: HTTPSERVLETREQUEST contains methods for obtaining HTTP-SPECIFIC header information .ServletResponse interface gives the corresponding client Servlet method. It allows servlet to set content length and response MIME type, and provide output streams, servletOutputStream, can send back the corresponding data by writers. ServletResponse subclasses can give more Protocol-Specific capacity information. For example: httpservletResponse contains methods that allow servlets to operate HTTP-SPECIFIC header information.

About classes and interfaces describes a basic servlet framework. HTTP servlets have some additional methods that provide session-tracking capabilities. Servlet writers can use these APIs to maintain the status between servlets and clients when there are others .

Servlet Lifecycle

Server load run servlets: Receive multiple requests from the client and return data to the client. Then delete the servlets. This is the servlets lifecle process. Detailed description below:

When a server loads servlet, it runs a servlet's init method. This method cannot be called repeatedly. Once the call is loaded, the servlet is loaded until the server calls the Destroy method to call the servlet before call.

After server loading initialization, servlet, servlet can handle the client's request. Use the service method to do this. Each client request has its own service method: These methods receive the client request and send back the corresponding response.

Servlets can run multiple service at the same time. This is important, so that the service method can be written in a Thread-Safe style. For example, the service method updates a field field in the servlet object, this field can be accessed at the same time. If a certain A server cannot run a service method at the same time, or you can use SingLethreadModel Interface. This interface is guaranteed to have more than two threads Threads.

Servlets has been running until they are uninstalled by the server. In Servlet's Lifecycle, write a Thread-SAFE encoding to uninstall servlet is important.

Write servlet

Servlets executes javax.servlet.servlet interface. When servlet writers develop servlets by direct import interface, this is usually not necessary. Because most servlets are for web servers with HTTP protocols, the most universal development servlet method is to use Javax. Servlet.http.httpservlet.

The HTTPSERVLET class performs servlet interface through the Extend GenericServlet class, provides the ability to process the HTTP protocol. His service method supports standard HTTP / 1.1 requests.

Generally, servlets written in the class specified by httpservlets can run the service method in multiple threads.

Interactivity with the client

Servlet Writer Note that there are several ways to lack your httpservlet class, you can define the contents of the method, but you must use these method names to make the servlet know what you want,

DOGET, used to process GET, conditional GET and header HEAD request DOPOST, user handles POST request DOPUT, to process the PUT request Dodelete, to process the delete request Httpservlet's service method, generally, when it receives an Options When the request is called, the DOOPTIONS method is called when receiving a TRACE request is called dotrace. Dooptions default execution method is automatically determined what kind of HTTP is selected and is returned which information.

When you use these methods, you must take two elaboration. The first data containing data from the client HTTPSERVLETREQUEST. The second parameter contains the client's response httpservletResponse. In the following example, this is the case.

An HTTPSERVLETREQUEST object provides access to the HTTP header, also allows you to get the client's data. How to get these data depends on the HTTP side request method.

Regardless of any HTTP mode, you can use the getParameterValues ​​method, this parameter value used to return a specific name. For the way with the HTTP GET request, this getQueryString method will return to anatomy. For anatomy, for anatomy, And the way you request it, you have two ways to choose. If you are text data, you can get BufferedReader through the getReader method; if you are binary data, you can get servletinputStream with the GetReader method.

In response to the client, an HttpservletResponse object provides two ways to return data to the user. You can return to the getWriter method, or the getOutputStream method returns to the output stream. You should use getWriter to return text data, and return binary data with GetOutputStream.

Before using Writer or OutputStream, the HTTP header should be set first. This method is provided in HTTPServletResponse, which can then use Writer or OutputStream to send a response body portion back to the user. After completion, turn off the Writer or Output Stream so that the server knows that the response has been complete.

An example of a HTTP Servlet handling GET and HEAD methods

Public class simpleServlet Extends httpservlet {

Public void doget (httpservletRequest Req, httpservletResponse res)

Throws ServleTexception, IOException

{

// First set the head

Res.SetContentType ("text / html");

// Return response data with Writer method

PrintWriter out = res. maxwriter ();

Out.println (" SimpleServlet Output </ title> </ head> <body>");</p> <p>Out.println ("<H1> SimpleET Output </ h1>");</p> <p>Out.println ("<p> this is output is from limited");</p> <p>Out.println ("</ Body>");</p> <p>Out.close ();</p> <p>}</p> <p>Public string getServletInfo () {</p> <p>Return "a Simple servlet";</p> <p>}</p> <p>}</p> <p>This example fully realized a servlet.</p> <p>An example of an HTTP Servlet handling the POST method</p> <p>Here is an example of using an HTML with a POST form: <HTML></p> <p><head> <title> jdcsurvey </ title> </ head></p> <p><body></p> <p><form action = http: // demo: 8080 / servlet / survey method = POST></p> <p><Input Type = Hidden Name = Survey Value = Survey01RESULTS></p> <p><BR> <BR> How Many Employees in your Company? <BR></p> <p><br> 1-100 <input type = radio name = Employee value = 1-100></p> <p><br> 100-200 <Input Type = Radio Name = Employee Value = 100-200></p> <p><br> 200-300 <Input Type = Radio Name = Employee Value = 200-300></p> <p><br> 300-400 <input type = radio name = EMPLOYEE VALUE = 300-400></p> <p><br> 500-more <input type = radio name = EMPLOYEE VALUE = 500-more></p> <p><BR> <BR> General Comments? <BR></p> <p><br> <input type = text name = comment></p> <p><BR> <BR> What IDEs do you use? <BR></p> <p><br> JavaWorkshop <input type = checkbox name = IDE value = javaworkshop></p> <p><br> J <input type = checkbox name = IDE value = j ></p> <p><br> Cafe '<Input Type = CheckBox Name = Ide Value = Cafe'></p> <p><BR> <BR> <input type = submit> <input type = reset></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>The servlet hereby writes form data into a file and responds to a user with a Thank you. The method of servlet, such as the following example:</p> <p>Public void dopost (httpservletRequest Req, httpservletResponse res)</p> <p>Throws ServleTexception, IOException</p> <p>{</p> <p>/ / First set the "Content Type" header of the response</p> <p>Res.SetContentType ("text / html");</p> <p>/ / Get the response PrintWriter to return to the client.</p> <p>PrintWriter TOCLIENT = Res. Getwriter ();</p> <p>Try {</p> <p>// Open a file to write the result of Survey.</p> <p>String Surveyname = Req.getParameterValues ​​("Survey") [0];</p> <p>FileWriter Resultsfile = New FileWriter (ResultsDir System.getProperty ("File.seParetor")</p> <p> Surveyname ".txt", true);</p> <p>PrintWriter Tofile = New PrintWriter (Resultsfile);</p> <p>// Get formats & storage from the client in this file</p> <p>Tofile.Println ("<begin>");</p> <p>Enumeration Values ​​= Req.getParameterNames ();</p> <p>While (VALUES.hasMoreElements ()) {</p> <p>String name = (string) VALUES.NEXTELEMENT ();</p> <p>String value = req.getParameterValues ​​(name) [0];</p> <p>IF ("Submit")! = 0) {</p> <p>Tofile.Println (Name ":" value);</p> <p>}</p> <p>}</p> <p>Tofile.println ("<end>");</p> <p>// Turn the file.</p> <p>Resultsfile.Close ();</p> <p>// Return to the client with a Thank you</p> <p>Toclient.println ("<HTML>");</p> <p>TOCLIENT.PRINTLN ("<Title> Thank you! </ Title>");</p> <p>TOCLIENT.PRINTLN ("Thank you for participating";</p> <p>TOCLIENT.PRINTLN ("</ html>");</p> <p>} catch (ioexception e) {</p> <p>E.PrintStackTrace ();</p> <p>TOCLIENT.PRINTLN</p> <p>"A problem occured while recording your answers."</p> <p> "Please try again.");</p> <p>}</p> <p>// Turn the Writer; the response is completed.</p> <p>TOCLIENT.CLOSE ();</p> <p>}</p> <p>This dopost method is to get data from the form to the GetParameterNameS and getParameterValues ​​method. Because it returns text to the client, dopost will call the getWriter method. Before the write response body part, it sets the setting of the header field, but After the response is completed, turn it off.</p> <p>Lifecycle method</p> <p>Re-editing initialization method</p> <p>During the initialization, servlet should prepare some resources it want to arrange so that this servlet can receive requests, do this can not consider multi-threaded, because the servlet initialization is only a single process. Once the initialization method is completed, the servlet can receive the client's request. Of course, if initialization can't succeed, this method will throw the Throw UnavailableException interpretation.</p> <p>Initialization method uses the servletconfig object as a parameter. This method should save this object so that it can have a method GetServletConfig returns. The easiest way is to make a new class, his initialization method calls super.init. If you do this, You should save the servletconfig object yourself and re-edit your GetServletConfig method so that it can get an object from the new location.</p> <p>The following is an example of an initialization method. It is an initialization method from the Survey Servlet. It receives an input from a form and stores it in the file. In order to store SURVEY information, it requires a directory. Receive this directory in initialization parameters .public void Init (servletconfig Config)</p> <p>Throws servletexception</p> <p>{</p> <p>Super.init (config);</p> <p>// Get a directory</p> <p>Resultsdir = GetInitParameter ("resultsdir");</p> <p>// If there is no directory, do not process the client</p> <p>IF (resultSdir == null) {</p> <p>Throw new unavailableException (this,</p> <p>"Not Given A Directory to Write Survey Results!");</p> <p>}</p> <p>}</p> <p>The initialization method here calls the super.init method to manage the Arrange ServletConfig object. This initialization method also sets a field: resultdir, as the directory name provided by the initialization parameter. If there is no directory name, this servlet throws an unable to explain If the initialization method is successfully completed, the servlet will process client requests.</p> <p>Initialization parameters</p> <p>The provisions of the initialization parameters are the provisions of a server.</p> <p>If the initialization parameter is specified, you can get: use the GetInitParameter method. This method uses the parameter name as its own parameter item.</p> <p>Reissue Destroy method</p> <p>When the server uninstalls a servlet, it will call the Destroy method of the servlet. This Destroy method is the opposite of the initialization method, and the servlet is released from the memory.</p> <p>Not all calling initialization init methods must also call the Destroy method.</p> <p>For most servlets, some initialization work must be done. For example, there is a servlet, which opens a database connection when initialization, his Destroy method displays: need to close this connection</p> <p>/ **</p> <p>* Turn off the database connection</p> <p>* /</p> <p>Public void destroy () {</p> <p>Try {</p> <p>C. close ();</p> <p>} catch (sqlexception e) {</p> <p>While (e! = null) {</p> <p>LOG ("SQLException:" E.GETSQLSTATE () '/ T' </p> <p>E.getMessage () '/ t' </p> <p>E.GetersRorcode () '/ t');</p> <p>E = E.GetNexTexception ();</p> <p>}</p> <p>} catch (exception e) {</p> <p>E.PrintStackTrace ();</p> <p>}</p> <p>}</p> <p>About a multi-thread involved in a servlet interrupt</p> <p>But a server uninstalls a servlet, which will call DESTROY after all the service has been completed. If your operation is running for a long time, but Destroy is called, there is a thread running. This servlet writer is responsible for ensuring all threads. All has been completed;</p> <p>Those servlets that run respond to client requests should keep how many methods currently run. His long-running method should poll cycle to ensure that they can continue to run. If servlet is called by the Destroy method, then this LONG-Running method If necessary, you must stop working or clear.</p> <p>For example, the variable serviceCounter is used to count how many service methods are running, and the variable shuttingdown displays whether this servlet is Destory. Each variable has its own acquisition method:</p> <p>Public shutdownexample extends httpservlet {</p> <p>Private int servicecounter = 0;</p> <p>Private bolean shuttingdown; ...</p> <p>// serviceCounter</p> <p>protected synchronized void enterserviceMethod () {</p> <p>ServiceCounter ;</p> <p>}</p> <p>protected synchronized void leavingserviceMethod () {</p> <p>ServiceCounter--;</p> <p>}</p> <p>protected synchronized int numservices () {</p> <p>Return serviceCounter;</p> <p>}</p> <p>// shuttingdown</p> <p>Protected setShuttingdown (Boolean flag) {</p> <p>Shuttingdown = flag;</p> <p>}</p> <p>Protected Boolean IsshuttingDown () {</p> <p>Return ShuttingDown;</p> <p>}</p> <p>}</p> <p>This service method has to be increased each time it enters, and it is reduced when it returns:</p> <p>Protected Void Service (HTTPSERVLETREQUEST REQ, HTTPSERVLETRESPONSE RESP)</p> <p>Throws ServleTexception, IOException</p> <p>{</p> <p>ENTERINGSERVICEMETHOD ();</p> <p>Try {</p> <p>Super.Service (Req, ResP);</p> <p>} finally {</p> <p>LeavingServiceMethod ();</p> <p>}</p> <p>}</p> <p>The Destroy method should check the serviceCounter. If there is a long-time operation, set the variable shuttingdown. This variable will let the thread that is processing the request know: This is over, close it! The Destroy method should wait for these service methods to complete, which is a clear shutdown process.</p> <p>Public void destroy () {</p> <p>/ * Check if there is a thread running, if there is, tell them STOP. * /</p> <p>IF (NumServices ()> 0) {</p> <p>SetshuttingDown (TRUE);</p> <p>}</p> <p>/ * Wait for them stop. * /</p> <p>While (NumService ()> 0) {</p> <p>Try {</p> <p>Thisthread.sleep (interval);</p> <p>} catch (interruptedexception e) {</p> <p>}</p> <p>}</p> <p>}</p> <p>If you need to check this variable, you will explain their work:</p> <p>Public void dopost (...) {</p> <p>...</p> <p>For (i = 0; ((i <lotsofstufftodo) &&! isshuttingdown ()); i ) {</p> <p>Try {</p> <p>PartoflongRunningOperation (i);</p> <p>} catch (interruptedexception e) {</p> <p>}</p> <p>}</p> <p>}</p> <p>Provide information about servlet</p> <p>/ **</p> <p>* This is a simple esample of an http servlet. It Responds to the get</p> <p>* And Head Methods of the Http Protocol.</p> <p>* /</p> <p>Public class simpleServlet Extends httpservlet {</p> <p>...</p> <p>Public string getServletInfo () {</p> <p>Return "a Simple servlet";</p> <p>}</p> <p>}</p> <p>How to run servlet with servletrunner</p> <p>Once you write your servlet, you can run on a lot of web servers or in servletrunner.</p> <p>Attributes</p> <p>The property is a pair of key-value, used as an initialization, creation, and servlet. For example, servlet.phone.code = phoneServlet is servlet.phone.code, his value is a phoneServlet. A servlet has two properties. One is servlet.name.code, his value is the class name of the servlet. The other is servlet.name.initargs, and his value is to save the initialization parameters for the servlet.</p> <p>Use the Code property</p> <p>Servlet.name.code properties Name your servlet with the name of its class. If your servlet uses initialization parameters, this property must be. It allows the server to join the servlet object and his initialization parameters, they have the same name Name Even if your servlet does not use the initialization parameters, it is recommended to use this property so that the client can reach servlet with its own name.</p> <p>The syntax of the initARGS attribute</p> <p>The value of servlet.name.initargs property is the value of the initialization parameter. The syntax of the corresponding parameter is: ParameterName = parameterValue. A Phone Servlet parameter image below:</p> <p>servlet.phone.initargs = /</p> <p>PhoneList = servlets / phonelist</p> <p>If there are multiple initialization parameters, they are separated from, such as:</p> <p>servlet.dbdemo.initargs = /</p> <p>Username = fill_in_the_user, /</p> <p>Password = fill_in_the_password, /</p> <p>Owner = fill_in_the_name</p> <p>Property file</p> <p>There is a "servlet.properties" in a file, although but servletrunner runs, you can specify another name, this file should save all the properties of the servlet to run. It should be plain text; you can be edited Create it. Here is an example:</p> <p># Phone servlet (Sample.html)</p> <p>servlet.phone.code = phoneservlet</p> <p>servlet.phone.initargs = /</p> <p>PhoneList = servlets / phonelist</p> <p># Bulletin Board Servlet</p> <p>servlet.bboard.code = bBoardServlet</p> <p># Order Entry Servlet</p> <p>servlet.dbDemo.code = OrdeeringRyServlet</p> <p>servlet.dbdemo.initargs = /</p> <p>Username = fill_in_the_user, /</p> <p>Password = fill_in_the_password, /</p> <p>Owner = fill_in_the_name</p> <p>Servlet Runner</p> <p>If you want to run your servlet on the web server, see the instructions for the corresponding server. This is only explained how to run the servlet in a servletrunner driver environment with the product.</p> <p>This servletrunner is a small drive tool that is multithreaded so that it can run multiple servlets. But it does not start automatically when the server is started. Because small, there is only a small resource overhead.</p> <p>This servletrunner is in the <jdk> / bin directory. It will have the following information with -help. :</p> <p>% ./bin/servletrunner -help</p> <p>USAGE: Servletrunner [Options]</p> <p>Options:</p> <p>-P port the port number to listen on</p> <p>-b backlog the listen backlog</p> <p>-m Max Maximum Number of Connection Handlers</p> <p>-t Timeout Connection Timeout in MilliseConds</p> <p>-D Dir Servlet Directory</p> <p>-r root document root directory</p> <p>-s filename servlet property file name</p> <p>-V Verbose Output</p> <p>%</p> <p>In order to see the default values ​​for these options, you can call it with the -v switch. This will start runner. This will stop after you get the message.</p> <p>% ./bin/servletrunner -v</p> <p>Server settings:</p> <p>Port = 8080</p> <p>Backlog = 50</p> <p>Max Handlers = 100</p> <p>TIMEOUT = 5000</p> <p>servlet dir =.</p> <p>Document dir =.</p> <p>Servlet propfile =.: servlet.properties</p> <p>Once servletrunner is executed, you can run them directly in your browser, just as follows:</p> <p>http://machine-name: port / servlet / servlet-name</p> <p>Here Servlet-Name corresponds to the name you already gave your servlet. For example, in order to run the Phone Servlet, His property servlet.phone.code = phoneServlet, you will use the following URL. (Suppose servletrunner runs in one and it Localhost, at the port 8080, this Phone Servlet resides in the servlet directory:</p> <p>Http: // localhost: 8080 / servlet / phone</p> <p>Another example, SURVEY servlet, as a result of the submission form. The corresponding servleturl is:</p> <p>Http: // demo: 8080 / servlet / survey</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-57973.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="57973" 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.044</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 = 'QfbRWgafwIjO_2F4kMy3SsIAveN1eNrUoIS1_2BtNKoeTGP_2FpOg9mLYitrHXLsIKKKvLLznC7DulBMP_2Bmesyq2k_2FYA_3D_3D'; 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>