今天写了个文本编辑器,借助ckeditor感觉还行,放到网上,大家一起分享爱一下。
首先是页面部分,只需引入ckeditor.js文件即可(通过文本编辑器写出来的动西后,最后通过html显示出来)。
写道
<!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>editor(文本编辑器)</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<form action="/Project_learn/EditorServlet" method="post">
<p><input type="text" name="title" /></p>
<p><textarea name="contents"></textarea></p>
<p><input type="submit" value="发布"/></p>
</form>
<script type="text/javascript">
CKEDITOR.replace( 'contents' );
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>editor(文本编辑器)</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<form action="/Project_learn/EditorServlet" method="post">
<p><input type="text" name="title" /></p>
<p><textarea name="contents"></textarea></p>
<p><input type="submit" value="发布"/></p>
</form>
<script type="text/javascript">
CKEDITOR.replace( 'contents' );
</script>
</body>
</html>
其次是服务端,获取html中输入的信息,然后通过网页形式显示出来。同时把生成的网页保存在/blog/ 文件夹中。
package com.erong.project.web;
写道
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class EditorServlet
*/
public class EditorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EditorServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
String content = request.getParameter("contents");
String filename = UUID.randomUUID().toString();
File file = new File(request.getRealPath("/")+"blog/"+filename+".html");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
StringBuffer sb = new StringBuffer();
sb.append("<!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>");
sb.append(title);
sb.append("</title></head><body>");
sb.append(content);
sb.append("</body></html>");
osw.write(sb.toString());
osw.close();
response.setCharacterEncoding("utf-8");
response.sendRedirect(request.getContextPath()+"/blog/"+filename+".html");
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class EditorServlet
*/
public class EditorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EditorServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String title = request.getParameter("title");
String content = request.getParameter("contents");
String filename = UUID.randomUUID().toString();
File file = new File(request.getRealPath("/")+"blog/"+filename+".html");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
StringBuffer sb = new StringBuffer();
sb.append("<!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>");
sb.append(title);
sb.append("</title></head><body>");
sb.append(content);
sb.append("</body></html>");
osw.write(sb.toString());
osw.close();
response.setCharacterEncoding("utf-8");
response.sendRedirect(request.getContextPath()+"/blog/"+filename+".html");
}
}