无为清净楼资源网 Design By www.qnjia.com

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://****.****/****";//存到数据库不会乱码
 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中的文字保存换行空格存到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
jsp,textarea,文字,保存,换行,空格,数据库

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。