0
これはコードで間違っていますか?私があればと答えのHttpServletを作りたいが、左側を」私が表示され、 『割り当てが他の最後に』変数でなければなりません、私は正しく、私はそれを修正することができますどのように、メソッドを宣言することはできません?メソッドdoPost、eclipseとサーブレットでのifの割り当て
package controladores;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AjaxController
*/
@WebServlet("/AjaxController")
public class AjaxController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AjaxController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain ");
PrintWriter out = response.getWriter();
String action = request.getParameter("action");
if(action.equals("demo1")){
String nombreCompleto = request.getParameter("nombreCompleto");
out.println("Hola " + nombreCompleto);
}
else if (action.equals("demo2")){
int a = Integer.parseInt(request.getParameter("numero1"));
int b = Integer.parseInt(request.getParameter("numero2"));
out.println(a + b);
} else(action.equals("demo3")){
int d = Integer.parseInt(request.getParameter("numa"));
int c = Integer.parseInt(request.getParameter("numb"));
out.println(c * d);
}
}
}
とindex.jspの
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Demo Ajax</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css" rel="stylesheet"/>
<link href="" rel="stylesheet"/>
<link type="text/css" rel="stylesheet" href="resources/css/miPrimerCSS.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#unHola').click(function(){
var nombreCompleto = $('#nombreCompleto').val();
$.ajax({
type:'POST',
data: {
nombreCompleto : nombreCompleto,
action: 'demo1'
},
url: 'AjaxController',
success: function(result){
$('#resultadoNombre').html(result);
}
});
});
$('#unaSuma').click(function(){
var numero1 = $('#numero1').val();
var numero2 = $('#numero2').val();
$.ajax({
type:'POST',
data: {
numero1 : numero1,
numero2 : numero2,
action: 'demo2'
},
url:'AjaxController',
success : function(result){
$('#resultadoSuma').html(result);
}
});
});
$('#unProducto').click(function(){
var numero1 = $('#numa').val();
var numero2 = $('#numb').val();
$.ajax({
type:'POST',
data: {
numa : numa,
numb : numb,
action: 'demo3'
},
url:'AjaxController',
success : function(result){
$('#resultadoProd').html(result);
}
});
});
});
</script>
</head>
<body>
<nav>
<div class="nav-wrapper">
<a href="#!" class="brand-logo"> Logo</a>
<ul class="right hide-on-med-and-down">
<li><a href="sass.html">Home</a></li>
<li><a href="badges.html">Pages</a></li>
<li><a href="badges.html">About us</a></li>
<li><a href="badges.html">Lab</a></li>
<!-- Dropdown Trigger -->
<!-- <li><a class="dropdown-button" href="#!" data-activates="dropdown1">Dropdown<i class="material-icons right">arrow_drop_down</i></a></li> -->
</ul>
</div>
</nav>
<div class="container">
<p class="z-depth-5">
<fieldset>
<h3>Un Hola</h3>
<form>
Introduce tu nombre <input type="text" id="nombreCompleto"><br>
<input type="button" value="Hola" id="unHola"><br>
<span id="resultadoNombre"></span>
</form>
</fieldset>
</div>
<br><br>
<div class="container">
<fieldset>
<h3>Una Suma</h3>
<form>
Numero 1 <input type="text" id="numero1"><br>
Numero 2 <input type="text" id="numero2"><br>
Resultado <span
id=resultadoSuma></span><br> <input type="button" value="Suma"
id="unaSuma">
</form>
</fieldset>
</div>
<div class="container">
<fieldset>
<h3>Un Producto</h3>
<form>
Numero 1 <input type="text" id="numero1"><br>
Numero 2 <input type="text" id="numero2"><br>
Resultado <span id=resultadoProd></span><br> <input type="button" value="Producto"
id="unProducto">
</form>
</fieldset>
</div>
<footer class="page-footer">
<div class="container">
<h5 class="white-text">Footer Content</h5>
</div>
<div class="footer-copyright">
<div class="container">
© 2014 Copyright Text
<a class="grey-text text-lighten-4 right" href="#!">More Links</a>
</div>
</div>
</footer>
</body>
</html>
エラーメートルをご提示くださいエッセージ –