使用MyEclipse编写Servlet
来源:互联网 发布:淘宝美工属于什么专业 编辑:程序博客网 时间:2024/06/08 13:42
一 步骤
1、src->new->Servlet
2、重写doGet()或者doPost()方法
3、部署运行
二 代码
1、HelloServlet
package servlet;
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 HelloServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public HelloServlet() {
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 {
System.out.println("处理Get请求...");
response.setContentType("text/html");
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.println("<strong>Hello Servlet!</strong><br>");
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 {
System.out.println("处理Post请求...");
response.setContentType("text/html");
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.println("<strong>Hello Servlet!</strong><br>");
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
}
}
2、index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>使用MyEclipse创建Servlet小例子</h1>
<hr>
<a href="servlet/HelloServlet">Get方式请求HelloServlet</a><br>
<form action="servlet/HelloServlet" method="post">
<input type="submit" value="Post方式请求HelloServlet"/>
</form>
</body>
</html>
三 运行结果
阅读全文
0 0
- 使用MyEclipse编写Servlet
- 使用MyEclipse编写servlet程序
- Servlet——使用myEclipse开发Servlet
- 使用MyEclipse来开发servlet
- myeclipse servlet的基本使用
- 完整的JSP+Servlet+MyEclipse编写的注册登录
- MyEclipse使用servlet的二个示例
- 学习笔记(4)-----使用MyEclipse开发servlet
- 7.学习使用MyEclipse开发servlet
- Myeclipse:使用Servlet中文出现乱码解决方案
- MyEclipse使用之编写编码提示设置
- 使用servlet编写生成验证码
- 编写servlet
- 编写Servlet
- 编写 servlet
- 关于MyEclipse+Tomcat下的JSP 与 Servlet 的使用
- Servlet的HelloWorld实例(使用MyEclipse 6.5)
- MyEclipse添加Oracle数据库并使用Servlet读取Oracle数据
- iptables规则中DNAT规则设置
- Hadoop的功能与优势
- JAVA序列化笔记
- hadoop大数据概述
- Class.forName()用法详解
- 使用MyEclipse编写Servlet
- windows上tomcat后台、无弹窗开机启动配置
- Servlet执行流程
- centos7 k8s自动安装脚本
- 密码分类
- CMD命令之快速把文件夹下的文件名写到TXT
- Java程序移植
- hibernate中save()和persist()的区别
- 系统安全卫士企业级安全架构shiro源码分析