我试图将我的japplet中的帖子数据发送到我的servlet" Connexion"但我放在我的servlet init方法和processRequest方法中的输出永远不会显示在我的控制台中。
这是我的japplet中的send方法调用
private void jButtonEntrerActionPerformed(java.awt.event.ActionEvent evt) {
try {
String infos = "login=" + URLEncoder.encode(FieldLogin.getText(), "UTF-8")
+ "&mdp=" + URLEncoder.encode(FieldMdp.getText(), "UTF-8")+"&action=Login";
//"http://localhost:8088/Web_Applic_Achats_2/Connexion"
SendPost("/Connexion", infos, getCodeBase());
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
}
sendPost方法定义(在抽象类Send中)
public static void SendPost(String dest, String infos, URL pageCourante)
{
try {
URL url = new URL (pageCourante, dest);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
try (OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream())) {
out.write(infos);
}
} catch (MalformedURLException ex) {
System.out.println("Err url: "+ex.getLocalizedMessage());
} catch (IOException ex) {
System.out.println("IO envoi: "+ex.getLocalizedMessage());
}
}
最后我的servlet Connexion
public class Connexion extends HttpServlet{
BeanBDAccess bdAccess= null;
ServletContext sc;
@Override
public void init() throws ServletException {
System.out.println("Hey init");
try {
bdAccess= new BeanBDAccessMysql("BD_SOCIETE", "root", "boulaite");
} catch (Exception ex) {
Logger.getLogger(Connexion.class.getName()).log(Level.SEVERE, null, ex);
}
sc = getServletContext();
}
@Override
public void destroy() {
if(bdAccess != null)
{
try {
bdAccess.FermerConnectionBd();
} catch (Exception ex) {
Logger.getLogger(Connexion.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println("Hey processRequest");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// .... code ....
}
}
修改
我跑了很多测试,发现我在http服务器监视器(netbeans)中没有我的参数或完整的请求uri。 但是当我在我的applet中输出url时,url就可以了。
上次修改
我不知道它是否会改变一些东西,但我在http服务器监视器中注意到我得到了304法规:退出未修改