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&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>  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我扶奶奶过哈登

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值