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:
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: