第一个servlet各种碰壁,新手求指教
我直接把代码贴出来吧
这个是前台网页
<body bgcolor = "cccfff">
<FORM method = "post" action = "aa">
<P>数据1<INPUT type = "text" name = "gr1"></P>
<P>数据1<INPUT type = "text" name = "gr2"></P>
<P>数据1<INPUT type = "text" name = "gr3"></P>
<P><INPUT type = "submit" value = "提交">
<INPUT type = "reset" value = "清除">
</P>
</FORM>
</body>
这个是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 aa extends HttpServlet {
protected void processRequest(HttpServletRequest request , HttpServletResponse response)throws
ServletException ,
IOException{
response.setContentType("text/html ; charset = UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>getParams</title>");
out.println("</head>");
out.println("<body>");
out.println(request.getParameter("gr1") + "<br>");
out.println(request.getParameter("gr2") + "<br>");
out.println(request.getParameter("gr3") + "<br>");
out.println("<html>");
out.println("<html>");
out.println("</body>");
out.println("</html>");
}
protected void doGet(HttpServletRequest request , HttpServletResponse response)throws ServletException , IOException{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request , HttpServletResponse response)throws ServletException , IOException{
processRequest(request,response);
}
}
这个是web.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>aa</servlet-name>
<servlet-class>aa</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aa</servlet-name>
<url-pattern>/aa</url-pattern>
</servlet-mapping>
</web-app>
问题是这样的 我点击 提交 按钮 理论上应该是跳到servlet显示出各个文本框的内容 不过我点击 提交 出现的却是下载页面 点击下载 然后aa就被下载下来了
------解决方案--------------------果断换一个试试看?
------解决方案--------------------你这个问题 我已经出现多次了。 是你编码方式写错了
response.setContentType("text/html ;