国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp實現textarea中的文字保存換行空格存到數據庫的方法

2020-07-26 23:16:00
字體:
來源:轉載
供稿:網友

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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text"     name="title" value="a" size="40">    </td>   </tr>   <tr>    <td>author&nbsp;&nbsp;<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(" ")) + "&nbsp"     + 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中的文字保存換行空格存到數據庫的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东丽区| 泰宁县| 乳山市| 焦作市| 哈尔滨市| 江陵县| 静安区| 仪征市| 盐山县| 松原市| 元朗区| 宣汉县| 罗甸县| 土默特右旗| 河源市| 阿瓦提县| 英吉沙县| 文水县| 济阳县| 五寨县| 大冶市| 迁西县| 新建县| 荆州市| 慈利县| 楚雄市| 睢宁县| 黄浦区| 彰化市| 荥经县| 安福县| 寿光市| 东城区| 招远市| 江城| 河池市| 太仆寺旗| 濮阳市| 永泰县| 乌兰浩特市| 桑日县|