一 步骤
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>
三 运行结果
相关推荐
网上问卷调查系统 使用myeclipse编写 javaBean+JSP+Servlet.zip 1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)正在做课程...
基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写...
此资源是比较完善的JSP项目了,是用在MyEclipse环境下编写的。
使用myeclipse开发网站使用tomcat作为服务器MySQL为数据库链接办法
企业人事系统,使用jsp+servlet+mysql编写,使用的IDE是Intellij IDEA 12,包含数据库备份sql文件. 主要模块包括: ①人事档案管理:户口状况、政治面貌、生理状况、合同管理等; ②考勤加班出差管理; ③人事变动:...
用myeclipse编写的一个聊天室,比较简单,适合初学者来参考学习,里面用到了servlet ajax等技术..
使用myeclipse编写jsp或者servlet时,点击发布环境按钮没反应
用myeclipse编写html,jsp,servlet等应注意的问题;以及整合tomcat,引进java doc文档等
编写jsp页面实现如下界面效果,然后交给servlet计算矩形的周长和面积,并输出结果。
myeclipse 写的新闻发布系统 基于jsp servlet javabean mvc模式 功能比较完善 需要求答的加我qq 345977373
网络相册,对图片进行上传,下载等功能,用MyEclipse编写,采用JSP技术。