JSP question and answer

xiaoxiao2021-03-06  46

Java Chinese Station

1. How to mix using JSP and SSI #InClude? In JSP, you can use the pure html: But if Data.inc contains JSP code, we can Use: <% @ include file = "data.inc"%> 2, how to execute a thread security JSP? Just add the following instructions <% @ page isthreadsafe = "false"%> 3, how JSP handles HTML Form Data? By the built-in Request object, as follows: <% string item = request.getParameter ("item"); int howmany = new integer ("Units")). IntValue ();%> 4, How to include a static file? Static includes the following: <% @ include file = "Copyright.html"%> Dynamic Contains the following: 5, in How to use annotations in JSP? Mainly four mid methods: 1. <% - with -%> 2. // 3. / ** and ** / 4.

6, how to perform browse redirection in JSP? Use as follows: response.sendredirect ("http://ybwen.home.chinaren.com/index.html"); Physically change the HTTP Header property, as follows: <% response.setstatus (httpservletResponse.sc_moved_permanently); string newlocn = "/ newpath / index.html"; response.setHeader ("location", newlocn);%> 7, how to prevent The output in the JSP or Servlet is not saved in Cache by Browser? Add the following scripts to the beginning of the JSP file: <% response.setheader ("cache-control", "no-store"); // http 1.1 response .SetHeader ("Pragma", "NO-Cache"); // http 1.0 response.setdateHeader ("expires", 0); // prevents caching at the proxy server%> 8, how to set cookie in JSP? Cookie is As part of the HTTP header, the following method can be set: <% cookie mycookie = new cookie ("Aname", "Avalue"); response.addcookie (MyCookie);%> 9, how to delete a cookie in JSP <% Cookie killmycookie = new cookie ("mycookie", null; killmycookie.setMaxage (0); killmycookie.setpath ("/"); response.addcookie (killmycookie);%> 10, in a JSP request processing How to stop the implementation of JSP as follows: <% IF (Request.getParameter ("Wen")! = Null) {// do something;}%> 11, how to define methods in JSP You can define methods, But you can't access JSP built-in objects directly, but Methods passing through parameters.

As follows: <%! Public string howbadfrom (httpsession sees = req.getsession (); ... return req.getRemotehost ();}%> <% out.print ("in General, Lao Lee is not bigdie ");%> <% = howbadfrom (request)%> 12, if Browser has closed cookies, how do I open session in JSP to track the use of URL rewriting, as follows: Hello1.jsp <% @ Page session = "true"%> <% integer Num = new integer (100); session.putValue ("num", num); string url = response.EncodeURL ("Hello2.jsp");%> > Hello2.jsp Hello2.jsp <% @ page session = "true"%> <% integer i = (integer) session.getValue ("num"); out.println ("Num Value in Session is " I.intValue ());%> 13 Use the SMTPCLIENT class to send email as follows. <% @ page import = "Sun.Net.smtp.smtpclient, java.io. *"%> <% string from = "ybwen@sina.com"; string to = "Hewenjun@yeah.net, lei @ WHO. com.cn "; try {smtpclient client = new smtpclient (" mail.xxxx.xxx "); client.from (from); client.to (to); printstream message = client.startMessage (); message.println (" TO: " to); Message.Println (" Subject: Sending Email from JSP! "); Message.Println (" This Was Sent From A JSP Page!); Message.println (); Message.Println ("Cool ! :-) "); Message.Println (); Message.Println (" Good Boy "); Message.Println (" IM in Genius.com "); Message.Println (); Client.CloseServer ();} catch (IOEXCEPTION E) {System.out.println ("Error Sending Email: E);}%> 14, I can call a JSP error page in the servlet? Of course, there is no problem, show how to control logic on a servlet control A JSP error page is called within the unit.

protected void sendErrorRedirect (HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) throws ServletException, IOException {request.setAttribute ( "javax.servlet.jsp.jspException", e);.. getServletConfig () getServletContext () getRequestDispatcher (errorPageURL) .forward (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) {try {// do something} catch (Exception ex) {try {sendErrorRedirect (request, response, "/ jsp / MyErrorPage.jsp", EX);} catch (Exception E) {E.PrintStackTrace ();}}} 15, how to communicate with the code segment below with the EJB SessionBean communication, JAVAX. .naming. *, javax.rmi.portableremoteObject, foo.accounthome, foo.account "%> <%! // Define a global reference to the sessionBeanHome interface instance for the global reference of the sessionBeanHome interface instance, public void jspinit () {// Get Home interface instance InitialContext cntxt = new InitialContext (); Object ref = cntxt.lookup ( "java: comp / env / ejb / AccountEJB"); accHome = (AccountHome) PortableRemoteObject.narrow (ref, AccountHome.cl ASS);}%> <% // instantiate sessionbean account acct = acchome.create (); // call remote method acct.dowhatever (...); // So, etc.%> 16, when I use a result Time, how to prevent fields of the field from "NULL" display in my HTML input text field? You can define a simple function to achieve the purpose, as follows: <%! String blanknull (string s) {return (s == NULL)? "" "" "" "" "" "" "" "," "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Download a file in the servlet or jsp (such as: binary, text, executable)? There is now two solutions: A: Using HTTP, B: In the servlet, you can do it by setting up CONTENTTYPE and STREAM using Java.IO package. For example: response.setContentType ("

Application / X-MSWORD "); then want to output a buffer to write some stuff. 18. When using the usebean flag to initialize the bean to accept the initialization parameters Use the following two tags: 19 Is the like a subroutine call JSP? Of course, use 21, when I recompile to a class used by my JSP, why JVM continues to use my Old class? <% @ Include file = "abc.jsp"%> and The previous one is static, and the latter is a dynamic containing 22, JSP Disadvantages? 1. Break the Java program without good state 2. Because the Connection Pool Engine does not support the Connection Pooling 3.Servlet Engine No standard 4.jsp and other scripting interactions 23, can JSP recursive calls? Of course Yes, if you submit to Form to this page 34, how to implement JSP internationalization? Provide a resource bundles property file for various versions 25, why write text files in JSP? Use the PrintWriter object, such as: <% @ Page Import = "java.io. *"%> <% string str = "Print Me"; string nameOfTextFile = "/usr/anil/imp.txt"; try {printwriter PW = new printwriter (new fileoutputstream (nameOfTextFile); PW.Println (STR); PW.Close ();} catch (ieException e) {Out.println (E.GetMessage ());}%> 26, how to include absolute path files in JSP? Use UrlConnection.

转载请注明原文地址:https://www.9cbs.com/read-80051.html

New Post(0)