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" 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,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中的文字保存换行空格存到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:浅谈mysql数据库中的换行符与textarea中的换行符 总结以上是内存溢出为你收集整理的jsp实现textarea中的文字保存换行空格存到数据库的方法全部内容,希望文章能够帮你解决jsp实现textarea中的文字保存换行空格存到数据库的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)