1。servlet代码:
如果用包直接加上包就好:例如 package lcy;
import javax.servlet.http.*;
import java.io.*;
public class HelloHttp extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
PrintWriter pw = response.getWriter();
pw.println(encoding("welcome java"));
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) {
this.doGet(request, response);
}
private String encoding(String str)
{
try
{
str = new String(str.getBytes("ISO-8859-1"),"GB2312");
}
catch(UnsupportedEncodingException uee)
{
System.out.println("" + uee.getMessage());
}
return str;
}
}
2。web.xml配置:
<servlet>
<servlet-name>helloHttp </servlet-name> //调用时的真实名字
<servlet-class>HelloHttp </servlet-class>//servlet名字,如果有包加上包名,例如:lcy.HelloHttp
</servlet>
<servlet-mapping>
<servlet-name>helloHttp</servlet-name> //调用时的真实名字
<url-pattern>/HelloHttp </url-pattern>//servlet的具体路径,根目录下要找到才行
</servlet-mapping>
3.地址栏输入
http://localhost:8080/helloHttp
即可执行