很多的jsp程序例子代码下载(jsp案例代码)

本文目录一览:

jsp页面如何实现下载文档

jsp页面下载文档是在jsp中有一个a标签 ,当用户点击a标签的时候下载文件。

一般采用href属性直接指向一个服务器地址,只要链接的文件存在,就会给出弹出保存对话框.

点击a标签 先执行onclick事件,再请求href中指向的地址。

前端jsp:

a href=”#” onclick=”javascript:downloadtest(‘${app.id}’)” id=”pluginurl” style=”color: #83AFE2;text-decoration:underline;”/a

然后在js中:

function downloadtest(id){

var url = “%=request.getContextPath()%/app/download” + “/” + id;

$(“#pluginurl”).attr(“href”,url);

}

后台处理下载逻辑的java代码:

/**

* 下载文件

* @param id appid

* @param response

*/

@RequestMapping(value=”/download/{id}”)

public void download(@PathVariable String id, HttpServletResponse response){

String filepath = “”;

Result result = appService.getAppById(id);

App app = (App) result.getMap().get(“app”);

if(app == null){

return;

}

filepath = app.getUrl();

File file = new File(filepath);

InputStream inputStream = null;

OutputStream outputStream = null;

byte[] b= new byte[1024];

int len = 0;

try {

inputStream = new FileInputStream(file);

outputStream = response.getOutputStream();

response.setContentType(“application/force-download”);

String filename = file.getName();

filename = filename.substring(36, filename.length());

response.addHeader(“Content-Disposition”,”attachment; filename=” + URLEncoder.encode(filename, “UTF-8”));

response.setContentLength( (int) file.length( ) );

while((len = inputStream.read(b)) != -1){

outputStream.write(b, 0, len);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

if(inputStream != null){

try {

inputStream.close();

inputStream = null;

} catch (IOException e) {

e.printStackTrace();

}

}

if(outputStream != null){

try {

outputStream.close();

outputStream = null;

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

jsp代码小例子。这是java代码。能运行。我想让这个在jsp里运行。怎么写?环境了。

在JSP中可以直接写JAVA代码,虽然很难看……

%

//你的JAVA代码

%

还有就是,在JSP页面打印字符串应该是out.print(),而不是System.out.print(strs)

out是JSP的内置对象,用于输出

另外,注意导入包,在头部文件,例如

%@ page contentType=”text/html; charset=utf-8″ language=”java” import=”java.util.*” errorPage=”” %

需要一个可以运行的JSP简单代码?

%@ page language=”java” import=”java.util.*” pageEncoding=”ISO-8859-1″%

%

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%”

titleMy JSP ‘index.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

/body

/html

jsp购物车代码

//shopping.html

html

headtitleshopping stor/title/head

body

form action=”carts.jsp” target=”post”

br

please select the item that you want to buy

br

select name=”item”

optionbook:old man and the sea

optionx-box game machine

optionmp3 player

optioncce

optionbook:jsp programming

optioncd “the endless love”

optiondvd “gone with the wind”

/select

br

input type=”submit” name=”submit” value=”add”

input type=”submit” name=”submit” value=”remove”

/form

/body

/html

——————————————————————

//carts.jsp

%@page contentType=”text/html;charset=ISO8859_1″ %

html

jsp:useBean id=”cart” scope=”session” class=”test.DummyCart”/

jsp:setProperty name=”cart” property=”*”/

%

cart.processRequest();

%

br

ol

you have chosen these items:

%

String []items=cart.getItems();

for(int i=0;iitems.length;i++)

{

%

li%=items[i] %/li

%

}

%

/ol

hr

%@include file=”shopping.htm” %

/html

———————————————————————//DummyCart.java

package test;

import javax.servlet.http.*;

import java.util.Vector;

import java.util.Enumeration;

public class DummyCart

{

Vector v = new Vector();

String submit=null;

String item= null;

private void addItem(String name)

{

v.addElement(name);

}

private void removeItem(String name)

{

v.removeElement(name);

}

public void setItem(String s)

{

item=s;

}

public void setSubmit(String s)

{

submit=s;

}

public String[] getItems()

{

String []s=new String[v.size()];

v.copyInto(s);

return s;

}

public void processRequest()

{

if(submit==null)

addItem(item);

if(submit.equals(“add”))

addItem(item);

else if (submit.equals(“remove”))

removeItem(item);

reset();

}

private void reset()

{

submit=null;

item=null;

}

}

———————————————————————-

上面是一个简单的例子,功能都能实现,对网页效果要求更漂亮些的可做一些修改。

编写用户注册于登录的JSP页面的全部程序代码

3个jsp文件,第一个是login.jsp,第二个是judge.jsp,第三个是afterLogin.jsp

%@ page language=”java” contentType=”text/html; charset=GB18030″

pageEncoding=”GB18030″%

%@ page import=”java.util.*” %

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

html

head

title登录页面/title

/head

body

form name=”loginForm” method=”post” action=”judgeUser.jsp”

table

tr

td用户名:input type=”text” name=”userName” id=”userName”/td

/tr

tr

td密码:input type=”password” name=”password” id=”password”/td

/tr

tr

tdinput type=”submit” value=”登录” style=”background-color:pink” input type=”reset” value=”重置” style=”background-color:red”/td

/tr

/table

/form

/body

/html

%@ page language=”java” contentType=”text/html; charset=GB18030″

pageEncoding=”GB18030″%

%@ page import=”java.util.*” %

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

html

head

title身份验证/title

/head

body

%

request.setCharacterEncoding(“GB18030”);

String name = request.getParameter(“userName”);

String password = request.getParameter(“password”);

if(name.equals(“abc”) password.equals(“123”)) {

%

jsp:forward page=”afterLogin.jsp”

jsp:param name=”userName” value=”%=name%”/

/jsp:forward

%

}

else {

%

jsp:forward page=”login.jsp”/

%

}

%

/body

/html

%@ page language=”java” contentType=”text/html; charset=GB18030″

pageEncoding=”GB18030″%

!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”

html

head

title登录成功/title

/head

body

%

request.setCharacterEncoding(“GB18030”);

String name = request.getParameter(“userName”);

out.println(“欢迎你:” + name);

%

/body

/html

jsp 中网站的首页源代码

这是最简单的一个例子,数据库要你自己建,用的是ACCESS

%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”” %

html

head

meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″

titleJSP连接Access数据库/title

style type=”text/css”

!–

.style1 {

font-size: 20px;

font-weight: bold;

}

/style

/headbody

div align=”center” class=”style1″JSP连接Access数据库/div

br

hr

p%

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); //载入驱动程序类别

Connection con = DriverManager.getConnection(“jdbc:odbc:jspdata”); //建立数据库链接,jspdata为ODBC数据源名称

//建立Statement对象

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

ResultSet rs = stmt.executeQuery(“select * from lyb”); //建立ResultSet(结果集)对象,并执行SQL语句

%

/p

p align=”center”NUMB1数据表中记录如下/p

table width=”640″ border=”1″ align=”center” bordercolor=”#7188e0″

tr bgcolor=”d1d1ff”

th width=”49″编号/th

th width=”90″姓名/th

th width=”126″E-mail/th

th width=”221″网站/th

th width=”80″QQ/th

/tr

%

while(rs.next())

{

%

tr bgcolor=”#f8f8f8″

th%= rs.getString(1) %/th

th%= rs.getString(2) %/th

th%= rs.getString(3) %/th

th bgcolor=”#f6f6f8″%= rs.getString(4) %/th

th%= rs.getString(5) %/th

/tr

%

}

rs.close();

stmt.close();

con.close();

%

/table

p align=”center”br

如果您能看到表格中的数据,说明连接数据库成功!/p

/body

/html

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/157788.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-18 20:02
下一篇 2024-11-18 20:02

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Python数据统计案例的实现

    Python作为一个高级编程语言,拥有着丰富的数据处理库和工具,能够快速、高效地进行各类数据处理和分析。本文将结合实例,从多个方面详细阐述Python数据统计的实现。 一、数据读取…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • 仓库管理系统代码设计Python

    这篇文章将详细探讨如何设计一个基于Python的仓库管理系统。 一、基本需求 在着手设计之前,我们首先需要确定仓库管理系统的基本需求。 我们可以将需求分为以下几个方面: 1、库存管…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29

发表回复

登录后才能评论