jsp实现textarea中的文字保存换行空格存到数据库的方法
程序员文章站
2022-12-04 19:18:39
uploadnews.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8"...
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>
换行函数在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> <%! // 字符处理函数 换行符变成<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";//存到数据库不会乱码 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实现textarea中的文字保存换行空格存到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: 爆囧,奇葩出没笑点多
下一篇: 四川省是如何得名的?探索四川省历史的由来