網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > 綜合知識 > 

在jsp中怎麼寫

欄目: 綜合知識 / 發佈於: / 人氣:9.04K
1. 在JSP中如何寫文字檔案

使用PrintWriter對象,如:

在jsp中怎麼寫

<%@ page import="java.io.*" %>

<%

String str = "print me";

String nameOfTextFile = "/usr/anil/imp.txt";

try {

PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));

pw.println(str);

pw.close();

} catch(IOException e) {

out.println(e.getMessage());

}

2. jsp怎麼在輸入框中寫默認提示資訊

//給input添加如下事件 一開始默認設定內容爲 請輸入姓名 獲得焦點輸入內容後 清空

$(function(){

var n = $('#name');

n.val('請輸入姓名');

n.focus(function(){

$(this).val('');

}).blur(function(){

$(this).val('請輸入姓名');

})

});focus是獲取焦點事件 即用戶輸入時清除提示資訊

或者給input標籤加上 placeholder屬性 該設定可以使得沒有輸入時顯示默認內容 輸入之後清除默認提示內容

<input name="username" placeholder="請輸入姓名"/>

3. JSP中如何寫輸出計算結果的語句

jsp腳本中寫入:

<%

int sum=0;

for(int i=1;i<=100;i++){

sum=sum+i;

}

%>

由於sum的值在這個頁面均有效,於是可以在你想顯示的地方輸出:

<%

out.println(sum);

%>

或者

<%=sum%>

或者

${sum}

以上三種方式都可以將sum輸出來.

4. 在Jsp一個頁面中 寫一個文字方塊和確定按鈕 當點擊確定後在另一個頁面

這個是往文字方塊裏輸入的頁面

<%@ page contentType="text/html;charset=GBK" %>

<html>

<body>

<form id="f1" name="f1" method="post" action="show.jsp">

<input type="text" name="username">

<input type="submit" value="確定">

</form>

</body>

</html>

這個是顯示的頁面

<%@ page contentType="text/html;charset=GBK" %>

<html>

<body>

<%=request.getParameter("username")%>

</body>

</html>

要啓動tomcat才能執行

我執行出來啦 可行!

5. jsp中顯示驗證碼的代碼怎麼寫

import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO;/*生成驗證碼圖片*/ public class MakeCertPic { //驗證碼圖片中可以出現的字元集,可以根據需要修改 private char mapTable[]={ 'a','b','c','d','e','f', 'g','h','i','j','k','l', 'm','n','o','p','q','r', 's','t','u','v','w','x', 'y','z','0','1','2','3', '4','5','6','7','8','9' };/* 功能:生成彩色驗證碼圖片 參數wedth爲生成圖片的寬度,參數height爲生成圖片的高度,參數os爲頁面的輸出流*/ public String getCertPic(int width,int height,OutputStream os){ if(width<=0) width=60; if(height<=0) height=20; BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //獲取圖形上下文 Graphics g = image.getGraphics(); //設定背景顏色 g.setColor(new Color(0xDCDCDC)); g.fillRect(0,0,width,height); //畫邊框 g.setColor(Color.black); g.drawRect(0,0,width-1,height-1); //隨機產生的驗證碼 String strEnsure = ""; //4代表4爲驗證碼,如果要產生更多位的驗證碼,則加大數值 for(int i = 0;i<4;++i){ strEnsure += mapTable[(int) (mapTable.length*Math.random())]; } //將認證碼顯示到圖像中,如果要生成更多位的驗證碼,增加drawString語句 g.setColor(Color.black); g.setFont(new Font("Atlantic Inline",Font.PLAIN,18)); String str = strEnsure.substring(0,1); g.drawString(str,8,17); str = strEnsure.substring(1,2); g.drawString(str, 20, 15); str = strEnsure.substring(2,3); g.drawString(str, 35, 18); str = strEnsure.substring(3,4); g.drawString(str, 45, 15); //隨機產生15個干擾點 Random rand = new Random(); for(int i=0; i<10; i++){ int x = rand.nextInt(width); int y = rand.nextInt(height); g.drawOval(x,y,1,1); } //釋放圖形上下文 g.dispose(); try{ //輸出圖形到頁面 ImageIO.write(image, "JPEG", os); }catch (IOException e){ return ""; } return strEnsure; } } makeCertPic.jsp頁面用於調用生成驗證碼圖片的JavaBean,並在客戶端顯示,原始碼如下:<%@page contentType="image/jpeg" %><%@page language="java" pageEncoding="utf-8"%><% String str = image.getCertPic(0,0,response.getOutputStream()); //將驗證碼存入session中 session.setAttribute("certCode",str);%> 下邊是登入頁面:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><meta ; charset=UTF-8">驗證碼測試登入頁面 <body>

Tags:JSP