`

jsp 使用

    博客分类:
  • jsp
阅读更多
http://www.runoob.com/jsp/jsp-actions.html   JSP
request.getRemoteAddr() 获取本机IP
Properties properties = System.getProperties();获取系统熟悉

JSP表达式  <%= 表达式 %>
JSP注释 <%-- 这里可以填写 JSP 注释 --%>
JSP指令
<%@ page ... %>   //定义网页依赖属性,比如脚本语言、error页面、缓存需求等等
        <%@ include ... %> //  包含其他文件     指令 <%@ include file="date.jsp" %>           <jsp:include page="date.jsp" flush="true" />  (动作)        
        <%@ taglib ... %>    //引入标签库的定义
JSP动作:
<jsp:useBean>动作元素  <jsp:useBean id="name" class="package.class" /> //属性:class type beanName
     <jsp:getProperty>动作元素  //name 要检索的Bean属性名称。Bean必须已定义。property表示要提取Bean属性的值
      <jsp:setProperty>动作元素 name property value param
      <jsp:forward> 动作元素 <jsp:forward page="Relative URL" />
     <jsp:element> 、 <jsp:attribute>、 <jsp:body>动作元素
   <jsp:text>动作元素


JSP 过滤器:1,/opt/tomcat6_bill/webapps/zou/WEB-INF/classes/LogFilter.class   
            2. LogFilter.class中引入了Java类,手动加入路径touch a.sh  
tomcat下运行Java代码
 #!/bin/sh
  
javac -cp .:/opt/apache-tomcat-7.0.62/lib/annotations-api.jar:/opt/apache-tomcat-7.0.62/lib/catalina-ant.jar:/opt/apache-tomcat-7.0.62/lib/catalina-ha.jar:/opt/apache-tomcat-7.0.62/lib/catalina.jar:/opt/apache-tomcat-7.0.62/lib/catalina-tribes.jar:/opt/apache-tomcat-7.0.62/lib/ecj-4.4.2.jar:/opt/apache-tomcat-7.0.62/lib/el-api.jar:/opt/apache-tomcat-7.0.62/lib/jasper-el.jar:/opt/apache-tomcat-7.0.62/lib/jasper.jar:/opt/apache-tomcat-7.0.62/lib/jsp-api.jar:/opt/apache-tomcat-7.0.62/lib/servlet-api.jar:/opt/apache-tomcat-7.0.62/lib/tomcat7-websocket.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-api.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-coyote.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-dbcp.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-i18n-es.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-i18n-fr.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-i18n-ja.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-jdbc.jar:/opt/apache-tomcat-7.0.62/lib/tomcat-util.jar:/opt/apache-tomcat-7.0.62/lib/websocket-api.jar *.java

   
  3./opt/tomcat6_bill/webapps/zou/WEB-INF/web.xml
  <?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

  <servlet>
      <servlet-name>hello</servlet-name>
      <servlet-class>com.HelloWorldWebSocketServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>hello</servlet-name>
      <url-pattern>/websocket/test</url-pattern>
    </servlet-mapping>
</web-app>


  4.查看log  /opt/tomcat6_bill/logs/catalina.out
           

JSP 与数据库
centos7安装mysql
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
service mysqld restart

         <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost/test"
               user="root"  password="zouhuiying"/>

        <sql:update dataSource="${snapshot}" var="result">
          INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
        </sql:update>

<sql:update dataSource="${snapshot}" var="count">
  DELETE FROM Employees WHERE Id = ?
  <sql:param value="${empId}" />
</sql:update>

         <sql:query dataSource="${snapshot}" var="result">
            SELECT * from Employees;
         </sql:query>
<c:forEach var="row" items="${result.rows}">
<tr>
   <td><c:out value="${row.id}"/></td>
   <td><c:out value="${row.first}"/></td>
   <td><c:out value="${row.last}"/></td>
   <td><c:out value="${row.age}"/></td>
</tr>
</c:forEach>


javabean  public void setAge(int age){
      this.age = age;



自定义标签
@Override
22     public void doTag() throws JspException, IOException {
23         //得到代表jsp标签体的JspFragment
24         JspFragment jspFragment = this.getJspBody();
25         
26         //得到jsp页面的的PageContext对象
27         //PageContext pageContext = (PageContext) jspFragment.getJspContext();
28         //调用JspWriter将标签体的内容输出到浏览器
29         //jspFragment.invoke(pageContext.getOut());
30         
31         //将标签体的内容输出到浏览器
32         jspFragment.invoke(null);



JSP的session应用与JSP操作数据库,Java重定向


<%=request.getSession().getAttribute("email")%>
<%=request.getSession().getAttribute("password")%>

<%@page import="java.sql.*"%>
<%
        String sqlpassword="";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "websocket1";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "zouhuiying";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            con.setAutoCommit(false);
            st = con.createStatement();
            String sql = "select password from myapp_chatusers where email ='"+emaill+"'";
            rs = st.executeQuery(sql);
           // rs = st.executeUpdate(sql);  
            // PreparedStatement   pre = con.prepareStatement(sql);
            // pre.executeUpdate();安全性高
            out.println("No  \tName");
            out.print("</br>");
            while (rs.next()) {
                sqlpassword=rs.getString(1);
                out.print(sqlpassword + "   \t");
                out.print("</br>");
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

%>
<%
if (sqlpassword.equals(passwordd)){
    out.print("ok");
    request.getSession().setAttribute("email",emaill);
    request.getSession().setAttribute("password",passwordd);
    response.sendRedirect("http://192.168.139.215:8080/demo/#/sidebarRight");
}else{
    out.print("wrong passwrod");
    response.sendRedirect("http://192.168.139.215:8080/demo/#/forms");
}
%>


从表单和URL中获取参数值:
<%=request.getParameter("username")%>


自己设置数据:
request.setAttribute("username",username);
request.getAttribute("username");
<%String aa=(String)request.getAttribute("username");%>
<%
 out.println(aa);
%>

注:从requset中取出的数据是对象要转化成字符串

session对象
<%=request.getSession().setAttribute("zou","huiying")%>
<%=request.getSession().getAttribute("zou")%>



JSP定义数组
<%
int accounts[];
accounts=new int[100];
accounts[3]=99;
out.print(accounts[3]);
%>


<%
String users[];
users=new String[100];
users[3]="zozu";
out.print(users[3]);
%>



每次刷新页面,i的值增加1
<%
if(request.getSession().getAttribute("i")==null){
    request.getSession().setAttribute("i",5);
}else{
    try{
        Integer myi=(Integer)request.getSession().getAttribute("i");
        request.getSession().setAttribute("i",(Integer)request.getSession().getAttribute("i")+1);
    }catch(Exception e){
        out.println(e);
    }
}
out.println(request.getSession().getAttribute("i"));
%>




对象转化为字符
(String)request.getSession().getAttribute("url")
分享到:
评论

相关推荐

    jsp使用自定义标签taglib分页系列——完整例子

    jsp使用自定义标签taglib分页系列——完整例子

    JSP使用文件上传插件uploadify .

    JSP使用文件上传插件uploadify ,内附带最新版本的uploadify。欢迎学习交流。本文章地址http://blog.csdn.net/tianba8/article/details/9953383,欢迎交流学习。

    jsp使用vue资源包.zip

    本资源包“jsp使用vue资源包.zip”聚焦于如何在JSP项目中集成Vue.js,特别是利用ElementUI这个流行的前端组件库。下面我们将详细探讨这些知识点。 1. **JSP(JavaServer Pages)**: JSP是Java的一种动态网页技术...

    jsp使用poi生成有格式的excel

    标题中的"jsp使用poi生成有格式的excel"指的是使用Java Server Pages (JSP) 技术,结合Apache POI库来创建带有特定格式(如样式、合并单元格、打印设置等)的Excel文件。Apache POI是Apache软件基金会的一个开源项目...

    JSP使用教程例题代码

    通过案例,读者可以学习如何在JSP中创建、初始化和使用Javabean。 **5. 文件操作** 第五章讲解了如何使用Java I/O流进行文件读写操作,并展示了如何通过Javabean实现这一功能。这对于构建需要处理文件上传或下载的...

    JSP使用ckeditor和ckfinder实现富文本及文件上传

    在这个场景中,"JSP使用ckeditor和ckfinder实现富文本及文件上传"是一个常见的需求,尤其是在构建内容管理系统或者论坛等需要用户编辑和上传内容的Web应用中。 ckeditor是一款流行的开源JavaScript富文本编辑器,它...

    蓝凌EKP jsp使用教程

    蓝凌EKP jsp使用教程

    JSP使用Fckeditor所用jar包

    本篇将详细介绍JSP使用Fckeditor所需的知识点以及JAR包的作用。 1. **FCKeditor介绍** FCKeditor是一款开源的富文本编辑器,支持多种浏览器,如Internet Explorer、Firefox、Chrome等。它提供了丰富的文本格式化...

    jsp中使用echarts示例

    **JSP中使用ECharts详解** ECharts是一款由百度开源的、基于JavaScript的数据可视化库,它提供了丰富的图表类型,如饼图、柱状图、折线图等,用于直观、生动地展示数据。在Java Web开发中,我们常常用JSP(Java...

    JSP使用FCKeditor详解

    JSP使用FCKeditor详解

    jsp使用URL编码传递中文参数乱码问题

    jsp使用URL编码传递中文参数乱码问题

    jsp使用JSON.stringify()引用的json2.js

    jsp使用JSON.stringify()引用的json2.js

    jsp 使用教程,能够很好的学习和使用jsp

    本教程将全面解析JSP的核心概念和使用方法,帮助你快速掌握这项技术。 ### 一、JSP基础 1. **JSP页面结构**:一个JSP页面由HTML标签、脚本元素和指令组成。HTML标签负责页面布局,脚本元素包括脚本let、声明、...

    jsp使用ATGridActiveX类开发报表

    jsp使用ATGridActiveX类开发报表,大大简化了报表开发工作量. ATGrid报表控件的使用,服务器端使用报表插件. 超经典报表开发代码,适合报表开发者和jsp初学者、自己开发jar包使用学习者

    JSP使用C标签时必须引入的jar包

    4. **在JSP页面中导入**:在JSP页面头部,使用以下指令引入C标签库: ```jsp &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; ``` 这里,`prefix="c"`定义了一个别名,后续在页面中就可以...

    jsp中使用vuejs+element-ui+vuejs第三方组件

    以上就是在JSP中使用Vue.js+Element-UI+Vue.js第三方组件的基本步骤。这样的组合使得开发者可以利用Vue.js的强大功能和Element-UI的精美组件库,同时保持JSP对后端数据的控制力,从而创建出高效、美观的Web应用。...

    jsp操作mysql简单实例

    在本实例中,我们将探讨如何使用JSP(JavaServer Pages)与MySQL数据库进行交互,实现基本的Web应用程序功能,如用户注册、登录、数据增删改查等。这个压缩包包含了一个简单的Web应用实例,提供了所需的JDBC驱动以及...

    jsp使用过程中经常出现的错误总结

    jsp使用过程中经常出现的错误总结,欢迎能够来下载。。。相信不会让你失望

    jsp实用教程上机实验

    4. 学习如何使用JDBC通过JSP连接数据库,执行查询和更新操作。 5. 掌握MVC模式,编写Servlet处理请求,JSP显示结果。 6. 部署和调试JSP应用,了解Web容器如Tomcat的工作原理。 通过这些实验,你不仅可以理解JSP的...

Global site tag (gtag.js) - Google Analytics