本文目錄一覽:
- 1、jsp頁面如何實現下載文檔
- 2、jsp代碼小例子。這是java代碼。能運行。我想讓這個在jsp里運行。怎麼寫?環境了。
- 3、需要一個可以運行的JSP簡單代碼?
- 4、jsp購物車代碼
- 5、編寫用戶註冊於登錄的JSP頁面的全部程序代碼
- 6、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