Okay nu har jeg ændret mere i koden, så nu får jeg beskeden når jeg trykker submit: "Welcome Mads". Problemet er at mine data ikke bliver registreret i databasen?
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
</head>
<body>
<form action="Registration" method="POST">
Firstname: <input type="text" name="firstname" value="">
Lastname: <input type="password" name="lastname" value="">
<input type="submit" value="submit"></input>
</form>
</body>
</html>
package myservlets;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Registration extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:
mysql://localhost/logindatabase"; Connection connection=null;
//ResultSet rs;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String fname = req.getParameter("firstname");
String sname = req.getParameter("lastname");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
String sql = "INSERT INTO login VALUES (?,?)";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setString(1, fname);
pst.setString(2, sname);
//int numRowsChanged = pst.executeUpdate();
out.println(" Welcome : ");
out.println(" '"+fname+"'");
pst.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns="
http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<description>
User Registration and Login Example.
</description>
<display-name>User Registration and Login Example</display-name>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>myservlets.Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/Registration</url-pattern>
</servlet-mapping>
</web-app>