gpt4 book ai didi

java - 如何使用input和post方法

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:09 24 4
gpt4 key购买 nike

我开始学习 java web,我需要了解为什么我的代码无法工作当我尝试提交代码时,我注意到当我转到应该进行更改的页面时,它没有显示任何内容。

我将展示一些代码

Post Method

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String titulo = request.getParameter("titulo");
String imagem = request.getParameter("imagem");
String descricao = request.getParameter("descricao");
String categoria = request.getParameter("categoria");

Noticias noticia = new Noticias();



System.out.println(titulo);
noticia.setTitulo(titulo);
System.out.println(descricao);
noticia.setDescricao(descricao);
System.out.println(categoria);
noticia.setCategoria(categoria);
System.out.println(categoria);
noticia.setImagem(imagem);

Noticias.dados.add(noticia);

response.sendRedirect(categoria);
}

Get Method

      @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

Inputs

<form>
<div class="form-group">
<label for="titulo">Titulo</label>
<input class="form-control" id="titulo" placeholder="Titulo">
</div>
<div class="form-group">
<label for="imagem">Imagem</label>
<input class="form-control" id="imagem" placeholder="Imagem">
</div>
<div class="form-group">
<label for="descricao">Descrição</label>
<input class="form-control" id="descricao" placeholder="Descrição">
</div>
<div class="form-group">
<label for="categoria">Categoria</label>
<input class="form-control" id="categoria" placeholder="Categoria">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

我的系统输出没有显示任何输出。并且页面上没有显示任何更改,我猜帖子没有被初始化

最佳答案

如果您要提交表单,则必须在表单标记中提及方法和操作属性。

   <form action="servletname" method="post">

该 servlet 也应该出现在 web.xml 中。

您可以点击此链接https://www.journaldev.com/1877/servlet-tutorial-java

关于java - 如何使用input和post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56719959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com