• Jsp开发入门


    安装Jdk,安装apache-tomcat
    假设tomcat安装路径C:\apache-tomcat-6.0.18\,tomcat配置端口80
    创建文件夹C:\apache-tomcat-6.0.18\webapps\jsptest,进入jsptest,
    创建文件WEB-INF\web.xml,
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
     </servlet>
    </web-app>
    在jsptest创建文件HelloWorld.jsp
    <%@ page contentType="text/html;charset=gb2312"%>
    <HTML>
      <HEAD>
          <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
          <TITLE>Hello World!</TITLE>
      </HEAD>
      <BODY>
          <% out.println("Hello World"); %> !
      </BODY>
    </HTML>
    通过C:\apache-tomcat-6.0.18\bin\startup.bat启动tomcat,在浏览器中输入网址http://localhost/jsptest/HelloWorld.jsp即可。

    Jsp取得主机名
    <% out.println(request.getHeader("Host")); %>
    页面输出localhost

    Jsp取得网址中传过来的参数
    比如输入网址http://localhost/jsptest/HelloWorld.jsp?username=123456
    <% out.println(request. getParameter ("username ")); %>
    页面输出123456

    使用java类
    比如使用org.jivesoftware.util.Blowfish进行解密,首先将文件Blowfish.class复制到WEB_INF\classes下,注意完整路径名应该为WEB-INF\classes\org\jivesoftware\util\Blowfish.class。


    在<%@ page contentType="text/html;charset=gb2312"%>之后添加代码
    <%@ page import="java.util.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.net.*" %>
    <%@ page import="org.jivesoftware.util.Blowfish" %>

    <%
    Blowfish bf  = new Blowfish("0V10u835HX4x0oiK88");
    bf.decryptString(password);
    %>

    Jsp中重定向
    比如<jsp:forward page="http://www.baidu.com"/>,将页面重定向至www.baidu.com。

    模拟网页登录
    String surl = "http://locahost/jsptest/HelloWorld!";
    URL url = new URL(surl);
    //建立连接,将读到的信息输出到当前网页
    URLConnection connection = url.openConnection();
    String sCurrentLine = "";
    String sTotalString = "";
    BufferedReader l_reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
    while ((sCurrentLine = l_reader.readLine()) != null)
    sTotalString += sCurrentLine + "\r\n";
    out.println(sTotalString);
    //使用javascript提交
    out.println("<script type=\"text/javascript\">\r\n");
    out.println("document.getElementsByName(\"name\")[0].value=\""+ params[1] +"\";\r\n");
    out.println("document.getElementsByName(\"pass\")[0].value=\""+ params[2] +"\";\r\n");
    out.println("document.forms[0].submit();\r\n");
    out.println("</script>\r\n");

    连接MySQL数据库
    首先将mysql-connector-java-5.1.6-bin.jar复制到WEB-INF\lib\下
    <%
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String url ="jdbc:mysql://localhost/jbpm?user=root&password=yourpass&characterEncoding=utf8";
    Connection conn= DriverManager.getConnection(url);
    Statement stmt = conn.createStatement();
    String sql = "select charset('abcd')";
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()){
    out.write(rs.getString(1));
    }
    out.print("数据库操作完成");
    rs.close();
    stmt.close();
    conn.close();
    %>

    连接Oracle数据库
    首先将ojdbc14.jar复制到WEB-INF\lib\下
    <%
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    String url ="jdbc:oracle:thin:@192.168.0.1:1521:dbname";
    Connection conn= DriverManager.getConnection(url,"username","password");
    Statement stmt = conn.createStatement();
    String sql = "select 1 from dual";
    ResultSet rs = stmt.executeQuery(sql);
    while(rs.next()){
    out.write(rs.getString(1));
    }
    out.print("数据库操作完成");
    rs.close();
    stmt.close();
    conn.close();
    %>
  • 相关阅读:
    滑雪在日本 之 新泻篇 7
    就算神游 之四:富士山和富士游乐园 12
    滑雪在日本 之 新泻篇 15
    就算神游 之四:富士山和富士游乐园 6
    滑雪在日本 之 新泻篇 6
    就算神游 之四:富士山和富士游乐园 13
    滑雪在日本 之 新泻篇 4
    20121022日记流水账
    滑雪在日本 之 新泻篇 8
    滑雪在日本 之 新泻篇 5
  • 原文地址:https://www.cnblogs.com/liuzhengdao/p/1970019.html
Copyright © 2020-2023  润新知