顯示具有 JSP 標籤的文章。 顯示所有文章
顯示具有 JSP 標籤的文章。 顯示所有文章

2013年6月15日 星期六

jsp include 範例

<%@ page contentType="text/html; charset=Big5" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>include的使用範列</title>
</head>

<body>
<font color="yellow">
<p>這是一個密碼檢查程式</p>

<jsp:include page="a1.jsp">
<jsp:param name="username" value="hyh" />
<jsp:param name="password" value="123456" />
</jsp:include>

<p>include之後的顯示,前面內容是include進來的</p>
<p>主網頁的內容顯示</p>
</font>
</body>

</html>

=================================================
<%@ page contentType="text/html; charset=Big5" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=big5">
<title>密碼檢查程式</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

if (username.equals("hyh")) out.println("使用者名稱正確<br>");
else out.println("使用者名稱錯誤<br>");
if (password.equals("123456")) out.println("密碼正確<br>");
else out.println("密碼錯誤<br>");
%>
<p>返回主程式</p>
</body>
</html>


ref :  Here

2013年4月15日 星期一

JSP 和MySQL的連結

1.建立一個Demo的資料庫,裡面有一個UserData的資料表

mysql> CREATE DATABASE Demo;  
(support 中文的建法:CREATE DATABASE Demo DEFAULT CHARACTER SET utf8)
mysql> Use Demo;  
mysql> CREATE TABLE UserData(  
UserID varchar(30)  NOT NULL,  
UserPassword varchar(80)  NOT NULL,  
UserName varchar(50)  NOT NULL,  
PRIMARY KEY  (UserID)  
);  
insert into userdata(UserID,UserPassword,UserName) values('Johnny','1111','Johnny');  
insert into userdata(UserID,UserPassword,UserName) values('Mary','2222','Mary');  
insert into userdata(UserID,UserPassword,UserName) values('Jenny','3333','Jenny');  
insert into userdata(UserID,UserPassword,UserName) values('Green','4444','Green');  
insert into userdata(UserID,UserPassword,UserName) values('Lily','5555','Lily');

2.連線之後取得裡面資料

<%@page contentType="text/html" pageEncoding="UTF-8"%>  
<%@page import="java.sql.*" %>  
<%  
Connection con = null;  
Statement stmt = null;  
ResultSet rs = null;  
    try {  
      String url = "jdbc:mysql://localhost:3306/demo";  
      String user = "root";  
      String password = "qwerfdsa";  
      String driver = "com.mysql.jdbc.Driver";  
      Class.forName(driver);  
      con = DriverManager.getConnection(url, user, password);  
      stmt = con.createStatement();  
      String sql = "select userid,userpassword,username from userdata order by userid";  
      rs = stmt.executeQuery(sql);  
    } catch (Exception ex) {  
      System.out.println(ex);  
    }  
%>  
<html>  
  <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Show All User</title>  
  </head>  
  <body>  
  <%  
    if(rs!=null){  
      %>  
      <table border="1">  
        <thead>  
          <tr>  
            <th>User ID</th>  
            <th>User Password</th>  
            <th>User Name</th>  
          </tr>  
        </thead>  
        <%  
        while(rs.next()){  
          String uid = rs.getString(1);  
          String upwd = rs.getString(2);  
          String uname = rs.getString(3);  
        %>  
        <tbody>  
          <tr>  
            <td><%=uid%></td>  
            <td><%=upwd%></td>  
            <td><%=uname%></td>  
          </tr>  
        </tbody>  
        <%  
        }  
        %>  
      </table>  
      <%  
    }  
  %>  
  </body>  
</html>

ref : Here

2013年4月14日 星期日

JSP - 將值傳到下一個頁面

abc.html
<form id="form" action="a.jsp">
<select name="sel" onChange="submit();">
    <option value="a">a
    <option value="b">b
    <option value="c">c
</select>
</form>
<script type="text/javascript">
function submit()
{
    document.getElementById("form").submit();
}
</script>

a.jsp

<%
    String str=request.getParameter("sel");
    out.print(str);
%>

ref: Here

2013年1月27日 星期日

JSP 字串處理

取代文字:str.replaceAll("a","b")
取出文字:str.substring(start index,end index)
字串長度:str.length()
比對字串:str.equals("字串")
轉成大寫:str.toUpperCase()
轉成小寫:str.toLowerCase()
轉成整數:Integer.parseInt(str)

ref: 巫老師的日誌

尋找特定字元:str.indexOf('字元');

ex:
<% 字串處理(JSP處理字串真的很麻煩  冏)
         String GPSaddr;
         String tmp_a, tmp_b;
         int symbol=0;
         int length=0;
         float a,b;
         GPSaddr=(String)request.getParameter("GPSaddr");
         symbol=GPSaddr.indexOf(',');
         length=GPSaddr.length();
         tmp_a=GPSaddr.substring(0,symbol);
         tmp_b=GPSaddr.substring(symbol+1,length);
         a=Float.valueOf(tmp_a);
         b=Float.valueOf(tmp_b);
%>

這個比較好用:
String splitString = "Bob:Stev:David:John";
String[] names = splitString.split(":");
for(String name:names)
{
    System.out.println(name);
}

ref: Cooking Java

create function

<%!
  public String test(String str, int sss)
  {
    String a, b;
    int symbol=0;
    int length=0;
    symbol=str.indexOf(',');
    length=str.length();
    a=str.substring(0,symbol);
    b=str.substring(symbol+1,length);
    if (sss==0)
        return a;
    else
        return b;
  }
%>

<%=test("25,31",1)%>


function裡查了資料,九成九不能用out.print =  =
怎麼有這麼不人性化的東東阿

/*nice*/