很多的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/zh-tw/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 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python數據統計案例的實現

    Python作為一個高級編程語言,擁有著豐富的數據處理庫和工具,能夠快速、高效地進行各類數據處理和分析。本文將結合實例,從多個方面詳細闡述Python數據統計的實現。 一、數據讀取…

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

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29

發表回復

登錄後才能評論