首页 > 编程知识 正文

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

时间:2023-12-24 12:05:50 阅读:321031 作者:PBIT

本文目录一览:

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。