Jsp簡單應用

==Jsp==:java sever pages

翻譯 編譯 執行

jsp可以重用編譯好的Java源碼,提高速度。

  • 註釋:

指令:

<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=GBK" %>

腳本:

<%
int numA = 4, numB = 5 ;
int result = numA+numB;
%>

表達式 打印輸出:

<%=result %>

方法體

<%!
String formatDate(Date d){
    SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日");
    retrun formater.format(d);
}
%>
<%=formatDate(new Date()) %>
<%--方法可以在頁面多處調用--%>

==Jsp內置對象==
- out session request application response
- ==requsert==:用於處理客戶端請求。

request對象方法 用處
getParameter 單個數據
ParameterValues 對應多個值時
setCharacterEncoding 請求的亂碼
getRemoterAddr() 返回客戶機地址

註冊login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'test04.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
   <form name="form1" method="post" action="reginfo.jsp">
    <table border="0" align="center">
      <tr> <td>用戶名</td><td> <input type="text" name="name"></td></tr>
      <tr><td>密碼</td><td > <input type="password" name="pwd"> </td></tr>
      <tr> <td>您的信息來源</td><td> 
          <input type="checkbox" name="channel" value="報刊">報刊 
          <input type="checkbox" name="channel" value="網絡">網絡 
          <input type="checkbox" name="channel" value="朋友推薦"> 朋友推薦 
          <input type="checkbox" name="channel" value="電視"> 電視
        </td></tr>
       <!-- 以下是提交、取消按鈕 -->
       <input type="submit" value="提交">
    </table>

</form>

  </body>
</html>

信息讀取reginfo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'reginfo.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    This is my JSP page.
    <br>
    <%
        request.setCharacterEncoding("utf-8");
        //讀取用戶名和密碼
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
    %>
    用戶名:<%=name%><br/>
    密碼:<%=pwd%><br/>
    信息來源:
    <%
        //讀取複選框選擇項
        String[] channels = request.getParameterValues("channel");
        if (channels != null) {
            for (int i = 0; i < channels.length; i++) {
                out.print("<br/>"+channels[i]);
            }
        }
    %>

</body>
</html>

亂碼

<%  ----以POST方式提交數據時----
    //設置讀取請求信息的字符編碼爲GBK或者GB2312
    request.setCharacterEncoding("GBK");    
    //讀取用戶名和密碼
    String name = request.getParameter("name");
    String pwd = request.getParameter("pwd");
%>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章