当前位置: 技术问答>java相关
servlet不能运行,大家帮忙看看
来源: 互联网 发布时间:2015-07-24
本文导语: web.xml里的配置: techsupport techsupport techsupport /techsupport ...
web.xml里的配置:
techsupport
techsupport
techsupport
/techsupport
classes目录中techsupport.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import sun.net.smtp.SmtpClient;
import com.wrox.util.*;
public class techsupport extends HttpServlet
{
String message,msgFrom ,msgTo,msgSubject;
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
getParameters(req);
if(!sendMail())
{
res.sendError(res.SC_INTERNAL_SERVER_ERROR,
"An error occured while attemption to access the main srver");
return;
}
HTML h =new HTML("XYZ corporation IT D epartment");
h.add(HTML.HEADING,"Your requet has been submitted",false);
out.println(h.getPage());
out.close();
}
private void getParameters(HttpServletRequest req)throws ServletException,IOException
{
StringBuffer tempStringBuffer=new StringBuffer(1024);
msgSubject="Tech Support Request";
msgTo="zrhk@21cn.com";
msgFrom=req.getParameter("txtEmail");
tempStringBuffer.append("From: ");
tempStringBuffer.append(req.getParameter("txtFirst"));
tempStringBuffer.append(" ");
tempStringBuffer.append(req.getParameter("txtLast"));
tempStringBuffer.append("n");
tempStringBuffer.append("Phone: ");
tempStringBuffer.append(req.getParameter("txtPhone"));
tempStringBuffer.append("n");
tempStringBuffer.append("Email: ");
tempStringBuffer.append(req.getParameter("txtEmail"));
tempStringBuffer.append("nn");
tempStringBuffer.append("Software: ");
tempStringBuffer.append(req.getParameter("ddlb_software"));
tempStringBuffer.append("n");
tempStringBuffer.append("OS: ");
tempStringBuffer.append(req.getParameter("ddlb_os"));
tempStringBuffer.append("n");
tempStringBuffer.append("Problem: ");
tempStringBuffer.append(req.getParameter("txtProblem"));
tempStringBuffer.append("n");
message=tempStringBuffer.toString();
}
private boolean sendMail()
{
PrintStream out;
SmtpClient send;
try
{
send=new SmtpClient("21cn.com");
send.from(msgFrom);
send.to(msgTo);
out=send.startMessage();
out.println("From: "+msgFrom);
out.println("To: "+msgTo);
out.println("Subject: "+msgSubject);
out.println("n--------------------------n");
out.println(message);
out.println("rn");
out.println("n--------------------------n");
out.flush();
out.close();
send.closeServer();
}
catch(IOException e)
{
log("Error occurred while sending mail",e);
return false;
}
return true;
}
}
编译完全通过了
可是在运行时
http://localhost:8080/techsupport时
出现如下错误
***********************************************************************
Apache Tomcat/4.0.4 - HTTP Status 405 - HTTP method GET is not supported by this URL
--------------------------------------------------------------------------------
type Status report
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
techsupport
techsupport
techsupport
/techsupport
classes目录中techsupport.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import sun.net.smtp.SmtpClient;
import com.wrox.util.*;
public class techsupport extends HttpServlet
{
String message,msgFrom ,msgTo,msgSubject;
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
getParameters(req);
if(!sendMail())
{
res.sendError(res.SC_INTERNAL_SERVER_ERROR,
"An error occured while attemption to access the main srver");
return;
}
HTML h =new HTML("XYZ corporation IT D epartment");
h.add(HTML.HEADING,"Your requet has been submitted",false);
out.println(h.getPage());
out.close();
}
private void getParameters(HttpServletRequest req)throws ServletException,IOException
{
StringBuffer tempStringBuffer=new StringBuffer(1024);
msgSubject="Tech Support Request";
msgTo="zrhk@21cn.com";
msgFrom=req.getParameter("txtEmail");
tempStringBuffer.append("From: ");
tempStringBuffer.append(req.getParameter("txtFirst"));
tempStringBuffer.append(" ");
tempStringBuffer.append(req.getParameter("txtLast"));
tempStringBuffer.append("n");
tempStringBuffer.append("Phone: ");
tempStringBuffer.append(req.getParameter("txtPhone"));
tempStringBuffer.append("n");
tempStringBuffer.append("Email: ");
tempStringBuffer.append(req.getParameter("txtEmail"));
tempStringBuffer.append("nn");
tempStringBuffer.append("Software: ");
tempStringBuffer.append(req.getParameter("ddlb_software"));
tempStringBuffer.append("n");
tempStringBuffer.append("OS: ");
tempStringBuffer.append(req.getParameter("ddlb_os"));
tempStringBuffer.append("n");
tempStringBuffer.append("Problem: ");
tempStringBuffer.append(req.getParameter("txtProblem"));
tempStringBuffer.append("n");
message=tempStringBuffer.toString();
}
private boolean sendMail()
{
PrintStream out;
SmtpClient send;
try
{
send=new SmtpClient("21cn.com");
send.from(msgFrom);
send.to(msgTo);
out=send.startMessage();
out.println("From: "+msgFrom);
out.println("To: "+msgTo);
out.println("Subject: "+msgSubject);
out.println("n--------------------------n");
out.println(message);
out.println("rn");
out.println("n--------------------------n");
out.flush();
out.close();
send.closeServer();
}
catch(IOException e)
{
log("Error occurred while sending mail",e);
return false;
}
return true;
}
}
编译完全通过了
可是在运行时
http://localhost:8080/techsupport时
出现如下错误
***********************************************************************
Apache Tomcat/4.0.4 - HTTP Status 405 - HTTP method GET is not supported by this URL
--------------------------------------------------------------------------------
type Status report
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
|
那当然了,你的servlet当中提供的方法是doPost方法,而不是doGet啊,你把那个doPost改成doGet试试,或是把另一页面form中的方法改成method="post"
|
你的servlet里面没有doGe方法所以,不能够运行,你可以吧你的form表单的method=get 改成method=post就可以了,
|
doGet() not doPost()
|
把doPost改为service