之前用的Scala的IDE,发现没有Web开发插件,所以重新下载了个J2EE的eclipse,然后安装scala插件。
还有准备好Tomcat,配置好Eclipse的web开发环境。
Tomcat我用的8,java也是8,eclipse是4.5.1 。
之后新建web项目,在web项目上右键->Configure->Add Scala Nature ,然后就可以使用scala了。
之后右键新建servlet,这个servlet是java的,基于注解的,然后run一下,就能正常访问这个servlet。
之后新建scala的class,
下面直接上代码。
项目结构:
First代码:
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代码:
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的依赖包。
以上就是这次的内容。