Myeclispe 利用Servlet在浏览器中输出 hello world 的源码
2016-11-06 16:31
387 查看
效果图如下:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class fucktest extends HttpServlet {
/**
* Constructor of the object.
*/
public fucktest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");//输出的为文本格式
response.setCharacterEncoding("UTF-8");//中文乱码解决
//json字符串
String jsonStr = "{\"name\":\"fly\",\"type\":\"虫子肥乃哦\"}";
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet Hello world</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.print("<br/>");//换行显示
out.println("<br/>以下是json的测试数据<br/>");
out.println(jsonStr);//此处打印json字符串
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
//response.setContentType("application/json; charset=utf-8");
response.setCharacterEncoding("UTF-8");
String jsonStr = "{\"name\":\"fly\",\"type\":\"虫子\"}";
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method测试数据");
out.println("\r\n");
out.println("以下是json的测试数据");
out.println(jsonStr);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class fucktest extends HttpServlet {
/**
* Constructor of the object.
*/
public fucktest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");//输出的为文本格式
response.setCharacterEncoding("UTF-8");//中文乱码解决
//json字符串
String jsonStr = "{\"name\":\"fly\",\"type\":\"虫子肥乃哦\"}";
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet Hello world</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.print("<br/>");//换行显示
out.println("<br/>以下是json的测试数据<br/>");
out.println(jsonStr);//此处打印json字符串
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
//response.setContentType("application/json; charset=utf-8");
response.setCharacterEncoding("UTF-8");
String jsonStr = "{\"name\":\"fly\",\"type\":\"虫子\"}";
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method测试数据");
out.println("\r\n");
out.println("以下是json的测试数据");
out.println(jsonStr);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
相关文章推荐
- Servlet笔记2--模拟Servlet本质、第一个Servlet程序、将响应结果输出到浏览器中
- 《Javaweb---用自定义标签在浏览器中输出hello world!》
- 利用Servlet发送pdf文件到浏览器
- 利用Linux内核编译输出信息构建内核源码树
- servlet中Response输出源码解析
- 通过servlet实现向浏览器输出验证码图片
- servlet向浏览器输出验证码图片
- servlet中Response输出源码解析
- 利用Servlet输出PDF
- Html表单提交到Servlet输出到页面乱码 Html使用的编码是UTF-8编码显示页面,之后使用form表单提交字段到Servlet中,Servlet将利用getParamer方法获得fo
- 利用flask将opencv实时视频流输出到浏览器
- 在myeclipse创建一个servlet,向浏览器输出“Hello servlet!!”.
- 利用Servlet在客户端输出PDF文件
- node.js 浏览器中输出 “hello world”
- Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件
- [JSP] 利用Filter解决Servlet输出中文乱码的问题
- 【java】servlet输出pdf文件到浏览器 教程
- springMvc源码学习之:利用springMVC随时随地获取HttpServletRequest等对象
- servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码
- 《JavaWeb---Servlet的简单例子》---响应浏览器的请求,向浏览器输出数据