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");
%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值