jsp实现textarea中的文字保存换行空格存到数据库的方法
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中的文字保存换行空格存到数据库的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持硕编程。
相关文章
- jsp+servlet实现文件上传与下载功能
- EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
- 在JSP中使用formatNumber控制要显示的小数位数方法
- 秒杀系统Web层设计的实现方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- JSP使用过滤器防止Xss漏洞
- 在JSP页面中动态生成图片验证码的方法实例
- 详解JSP 内置对象request常见用法
- 使用IDEA编写jsp时EL表达式不起作用的问题及解决方法
- jsp实现局部刷新页面、异步加载页面的方法
- Jsp中request的3个基础实践
- JavaServlet的文件上传和下载实现方法
- JSP页面的静态包含和动态包含使用方法


