java servlet - Registration issue -
package jdbc; import java.io.ioexception; import java.io.printwriter; import java.sql.*; import static java.lang.system.out; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class registration extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try { string name = request.getparameter("name"); string email = request.getparameter("email"); string pass = request.getparameter("password"); string sql="insert regis(name,email,password) values(?,?,?)"; class.forname("com.java.mysql.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/login","root","3839"); preparedstatement ps= con.preparestatement(sql); ps.setstring(1,name); ps.setstring(2, email); ps.setstring(3, pass); ps.executeupdate(); printwriter out= response.getwriter(); out.println("registration done !!"); } catch (ioexception | classnotfoundexception | sqlexception ex) { out.println(ex); } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"; } }
i beginner in this, have done first have created 2 jsp login & register , 1 servlet (as code above), did mapping in web.xml , had connected mysql server (connector , jdbc driver ). still when enter data in register page , submit show (//localhost:8084/webapplication5/registration) nothing blank page, didnt show exception also.
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <display-name>registration </display-name> <servlet> <servlet-name>reg</servlet-name> <servlet-class>jdbc.registration</servlet-class> </servlet> <servlet-mapping> <servlet-name>reg</servlet-name> <url-pattern>/registration</url-pattern> </servlet-mapping> </web-app>
you're setting content type of response "text/html;charset=utf-8", content you're writing text/plain.
besides, highly recommend use datasource define in configuration of servlet container.
update:
try this:
response.setcontenttype("text/plain;charset=utf-8"); try (printwriter out = response.getwriter()) { try { string name = request.getparameter("name"); string email = request.getparameter("email"); string pass = request.getparameter("password"); string sql = "insert regis(name,email,password) values(?,?,?)"; class.forname("com.java.mysql.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/login", "root", "3839"); preparedstatement ps = con.preparestatement(sql); ps.setstring(1, name); ps.setstring(2, email); ps.setstring(3, pass); ps.executeupdate(); out.println("registration done !!"); } catch (ioexception | classnotfoundexception | sqlexception ex) { out.println(ex); } }
update 2:
my mistake: writer doesn't need closed:
response.setcontenttype("text/plain;charset=utf-8"); printwriter out = response.getwriter(); try { string name = request.getparameter("name"); string email = request.getparameter("email"); string pass = request.getparameter("password"); string sql = "insert regis(name,email,password) values(?,?,?)"; class.forname("com.java.mysql.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/login", "root", "3839"); preparedstatement ps = con.preparestatement(sql); ps.setstring(1, name); ps.setstring(2, email); ps.setstring(3, pass); ps.executeupdate(); out.println("registration done !!"); } catch (ioexception | classnotfoundexception | sqlexception ex) { out.println(ex); }
Comments
Post a Comment