jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法
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ù)庫不會亂碼 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實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計的實現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法