jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法
更新時(shí)間:2018年01月22日 09:50:53 作者:我扶奶奶過哈登
下面小編就為大家分享一篇jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
UploadNews.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>上傳新聞</title> <SCRIPT LANGUAGE="JavaScript"> function upload(){ document.getElementById("article").value = document .getElementById("content").value; document.getElementById("formid").submit(); } </SCRIPT> </head> <body> <form method="post" action="ShangchuanNews.jsp" id="formid"> <table border="0" align="center"> <tr> <td>title <input type="text" name="title" value="a" size="40"> </td> </tr> <tr> <td>author <input type="text" name="author" size="40"> </td> </tr> <tr> <td><input type="hidden" id="article" name="articleName" /></td> </tr> <tr> <td>date(xxxx.xx.xx)<input type="text" name="date" size="40"> </td> </tr> <tr> <td><div align="center"> <input type="button" value="submit" class="btn2" onclick = "upload();" /> </div></td> </tr> <tr> <td><textarea rows="30" cols="80" id="content"></textarea></td> </tr> </table> </form> </body> </html>
換行函數(shù)在ShangchuanNews.jsp 代碼如下
<%@page import="java.io.PrintWriter"%> <%@page import="java.net.URLDecoder"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; utf-8"> <title>上傳新聞</title> </head> <body> <%! // 字符處理函數(shù) 換行符變成<br> public String turn(String str) { while (str.indexOf("\n") != -1) { str = str.substring(0, str.indexOf("\n")) + "<br>" + str.substring(str.indexOf("\n") + 1); } while (str.indexOf(" ") != -1) { str = str.substring(0, str.indexOf(" ")) + " " + str.substring(str.indexOf(" ") + 1); } return str; } %> <% try { request.setCharacterEncoding("utf-8"); String title = request.getParameter("title"); String author = request.getParameter("author"); String article = request.getParameter("articleName"); String articlebr = turn(article); String date = request.getParameter("date"); String driverClass="com.mysql.jdbc.Driver"; String url = "jdbc:mysql://****.****/****?characterEncoding=utf8";//存到數(shù)據(jù)庫不會(huì)亂碼 String user="***"; String password="****"; Connection conn; int i=0; Class.forName(driverClass).newInstance(); conn = DriverManager.getConnection(url,user,password); String sql = "insert into news (id,title,author,article,date) " + "values(?,?,?,?,?)"; Connection conn1 = DriverManager.getConnection(url, user, password); PreparedStatement pstmt; pstmt = (PreparedStatement) conn1.prepareStatement(sql); pstmt.setString(1, null); pstmt.setString(2, title); pstmt.setString(3, author); pstmt.setString(4, articlebr); pstmt.setString(5, date); i = pstmt.executeUpdate(); conn1.close(); pstmt.close(); out.println("<br>上傳成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } %> </body> </html>
以上這篇jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法
- jsp 使用request為頁面添加靜態(tài)數(shù)據(jù)的實(shí)例
- Struts2.5 利用Ajax將json數(shù)據(jù)傳值到JSP的實(shí)例
- JSP 自定義標(biāo)簽實(shí)現(xiàn)數(shù)據(jù)字典的實(shí)例
- 讀取數(shù)據(jù)庫的數(shù)據(jù)并整合成3D餅圖在jsp中顯示詳解
- springMVC如何將controller中數(shù)據(jù)傳遞到j(luò)sp頁面
- jsp中EL表達(dá)式獲取數(shù)據(jù)
- JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
相關(guān)文章
FCKeditor使用方法(FCKeditor_2.6.3)詳細(xì)使用說明
要用到文本編輯器,選擇了FCKeditor,下面就配置作一下說明: 環(huán)境:windowsXP myeclipse6.0GA fckeditor2.6.3 fckeditor2.32008-09-09Java之JSP教程九大內(nèi)置對(duì)象詳解(上篇)
這篇文章主要介紹了Java之JSP教程九大內(nèi)置對(duì)象詳解(上篇),本文章內(nèi)容詳細(xì),通過案例可以更好的理解JSP內(nèi)置對(duì)象的相關(guān)知識(shí),本模塊分為了三部分,本次為上篇,講解了三個(gè)內(nèi)容,需要的朋友可以參考下2023-01-01基于JSP的動(dòng)態(tài)網(wǎng)站開發(fā)技術(shù)
基于JSP的動(dòng)態(tài)網(wǎng)站開發(fā)技術(shù)...2006-10-10