1
On the college computer I used this code to connect Java to the Web. It worked, but at home, I have a more current version of Eclipse and the code behaved differently. It does not recognize the import javax.servlet. With this make a mistake:
package br.edu.unicid.web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.edu.unicid.bean.Aluno;
import br.edu.unicid.dao.AlunoDAO;
/**
* Servlet implementation class ServletAluno
*/
@WebServlet("/ServletAluno")
public class ServletAluno extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// a variável cmd indica o tipo de ação - incluir, alterar, consulta.....
String cmd = request.getParameter("cmd");
// cria um objeto dao - CRUD
AlunoDAO dao;
// cria um objeto do tipo aluno
Aluno aluno = new Aluno();
if (cmd != null) {
try {
// inicializa os atributos da classe Alunos
aluno.setCaAluno(Integer.parseInt(request.getParameter("txtCa")));
aluno.setNomeAluno(request.getParameter("txtNome"));
aluno.setEmailAluno(request.getParameter("txtEmail"));
aluno.setDtaNasc(request.getParameter("txtData"));
aluno.setIdadeAluno(Integer.parseInt(request.getParameter("txtIdade")));
aluno.setEndAluno(request.getParameter("txtEndereco"));
} catch (Exception ex) {
ex.printStackTrace();
}
}try {
// cria a instancia do objeto dao
dao = new AlunoDAO();
RequestDispatcher rd = null;
// incluir aluno
if (cmd.equalsIgnoreCase("incluir")) {
dao.salvar(aluno);
}
// rd.forward(request,response);
// executa a ação de direcionar para a página JSP
}catch (Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
}
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);
}
}
This is because in school you must be using Java EE and in your home you must be using Java SE.
– Augusto Vasques