您的位置:首页 > 编程语言 > Java开发

自己写个servlet,想集成到别人原有的项目中,实现自己想要的功能(比如我想写个ajax的功能,把这个servletAjax.java的类编译成.class文件)

2014-07-28 20:02 866 查看
自己写个servlet,想集成到别人原有的项目中,实现自己想要的功能(比如我想写个ajax的功能,把这个servletAjax.java的类编译成.class文件)

摸索实践后现在总结如下:

【1】 你首先有个web项目(别人的原有项目或自己新建个都行)比如叫myweb项目。存放在D:/Documents/java/bin/apache-tomcat-6.0.37/myweb;

【2】 在tomcat中,添加conf 下的server.xml中稍微配置下:

<Context path="/web" reloadable="true" docBase="D:/Documents/java/bin/apache-tomcat-6.0.37/myweb"/>

【3】 下来是你编写的servletAjax.java文件了

package com;-----------------------------------此处的导包必须按路径写正确!!我加个文件夹com

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 ServletAjax extends HttpServlet {

/**

* Constructor of the object.

*/

public ServletAjax() {

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 {

doPost(request, response);

}

/**

* 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/text;charset=UTF-8");

//String name=request.getParameter("uname");

//String pwd=request.getParameter("pwd");

PrintWriter out = response.getWriter();

String str="aaaaaaaaaaaaaaaaaaaaa";

out.println(str);

System.out.println("aaaaaaaaaa");

out.flush();

out.close();

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

【4】把上面这个.java 文件拿去编译成.class文件。成功后进行如下:

把.class文件放到项目的web-inf---------->> classes里面 --->>com ---ServletAjax.class

【5】配置web.xml 在项目的web-inf目录下 (就直接照抄行了,格式也照抄。就一个com文件夹就写一个行了,)

<web-app>

<servlet>

<servlet-name>ServletAjax</servlet-name>

<servlet-class>com.ServletAjax</servlet-class>

</servlet>

<servlet-mapping>

<servlet-mapping>

<servlet-name>ServletAjax</servlet-name>

<url-pattern>/ServletAjax</url-pattern>

</servlet-mapping>

</web-app>

【6】启动tomcat,输入http://localhost:8080/myweb/ServletAjax 就Ok

页面输出aaaaaaaaaaaa;

你的.java文件随便你放哪就行,主要是.class文件放到WEB-INF ==classes==com==servletAjax.class

如果你碰到这情况:


这是你的package com; 没写。或者是写错了。

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面介绍下如何把.java文件编译成.class文件。

classpath环境变量里要有 servlet-api-jar文件必须的。(;%CATALINA_HOME%\D:\Documents\apache-tomcat-6.0.37\lib\servlet-api.jar;)

同时把 servlet-api.jar 和jsp-api.jar 放到tomcat的lib 里面去。

配置这些完后就写个.java的文件了,上面有

测试下:

cmd 黑框后 打上d:盘符

就向这样的如下图片:就好了,虽然有错,那是我的.java文件里没main 方法,没事的,这样你会看到名字为c 的文件下里ServletAjax.java下面倏地一下多了个ServletAjax.class文件,这就对了, 然后你把这.class文件复制到项目的WEB-INF-----classes-----com(你的文件夹)------.class文件



这javac 就是编译成.class了, 没出错,就继续用 java命令 去执行他, 我的虽然出现了这些看似错误,其实好着哩。

http://localhost:8080/(你的web工程名字)/ServletAjax 就行了,不要多加任何路径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐