以前用的Scala的IDE,發現沒有Web開發插件,因此從新下載了個J2EE的eclipse,而後安裝scala插件。java
還有準備好Tomcat,配置好Eclipse的web開發環境。web
Tomcat我用的8,java也是8,eclipse是4.5.1 。app
以後新建web項目,在web項目上右鍵->Configure->Add Scala Nature ,而後就能夠使用scala了。eclipse
以後右鍵新建servlet,這個servlet是java的,基於註解的,而後run一下,就能正常訪問這個servlet。ide
以後新建scala的class,this
下面直接上代碼。插件
項目結構:scala
First代碼:blog
package day02; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class First */ @WebServlet("/First") public class First extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public First() { // 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 response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
ServletA代碼:ip
package day02 import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(Array("/A")) class ServletA extends HttpServlet { override def doGet(req: HttpServletRequest, res: HttpServletResponse) { res.getWriter().print("hello,this is a servlet written by scala"); } }
部署的時候可能須要單獨加上scala的依賴包。
以上就是此次的內容。